关闭具有导航控制器的视图控制器并将值传递回主视图控制器

关闭具有导航控制器的视图控制器并将值传递回主视图控制器

问题描述:

我在那个名为 get value 的按钮中有一个 mainViewcontroller.然后我调用 dataviewcontroller 来选择集合视图单元格中的任何项目.一旦用户选择任何单元格.那特定的 dataviewcontroller 将关闭,在关闭时它将具有用户选择的项目名称,并将显示在 mainViewcontroller 中.现在视图控制器不会关闭.

I have an mainViewcontroller in that one button called get value.Then i am calling dataviewcontroller to select any item in collection view cell.Once user select any cell.That particular dataviewcontroller will dismiss and while dismiss it will have the user selected item name and it will display in mainViewcontroller .Now the view controller is not dismissing.

代码如下:

在我的 mainViewcontroller 中:

var SelectedName: String? = ""

 override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        currentTF.text = SelectedName

    }

现在 dataviewcontroller :

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        if collectionView.cellForItem(at: indexPath) != nil {

            selectedNamesss = allitems[indexPath.row] as String
        }

        calldismiss()
      print(selectedNamesss)

    }

func calldismiss() {


        if let presenter = presentingViewController as? mainViewcontroller {
            presenter.SelectedName = selectedNamesss
        }
        dismiss(animated: true, completion: nil)
        }

现在我该如何解决这个问题.我的 viewcontroller 没有关闭,值也没有显示.

Now how can i solve this.My viewcontroller is not dismising and values also not showing.

提前谢谢~

我的建议是使用UnwindSegue"技术,它就像PerformSegue"一样简单,所以你可以很容易地使用prepare for segue发回值,并将回到主控制器.这里是 unwind segue 的教程.或者您可以从 google 找到更多信息.

My Suggestion is to use "UnwindSegue" technique, which is simply works like "PerformSegue", So you can send back values very easily using prepare for segue and will be back to main controller. Here is tutorial for unwind segue. Or you can find more from google.