Dismiss the view controller which have navigation controller and pass the value back to main view controller

混江龙づ霸主 提交于 2019-12-24 18:51:40

问题


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.

Here the code :

In my mainViewcontroller:

var SelectedName: String? = ""

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

        currentTF.text = SelectedName

    }

Now 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)
        }

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

Thanks in advance ~


回答1:


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.




回答2:


For The problem where data is not showing, problem is with this condition:

if let presenter = presentingViewController as? mainViewcontroller {
            presenter.SelectedName = selectedNamesss
        }

this condition will never be true as the presentingViewController would always be a UINavigationController. so you need to change that may be something like follows:

if let presenter = (presentingViewController as! UINavigationController).viewControllers.first! as? mainViewcontroller {
            presenter.SelectedName = name
        }

Here i am using viewControllers.first! in condition as mainViewcontroller was at first index of the viewControllers array. Check the viewControllers array and find the index of mainViewcontroller and make changes accordingly.

I made this changes and the code is working for me. I am getting the selected name on the mainViewcontroller.

Also i would like to mention that this is not the right way to transfer data backwards. You can use delegates, blocks or unwind segue to achieve this.

Though this is working i am attaching a gif to show this working.

Regarding the issue where your controller is not being dismissed, i am presenting the controller in following way:

let dataVC = self.storyboard?.instantiateViewController(withIdentifier: "dataviewcontroller") as! dataviewcontroller
 self.present(vc, animated: true, completion: nil)

Try this and see if this helps you :)



来源:https://stackoverflow.com/questions/46976309/dismiss-the-view-controller-which-have-navigation-controller-and-pass-the-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!