简体   繁体   中英

Dismiss current modal view controller and then present new modal view controller

I'm trying to dismiss a modal controller then present a new model controller by pressing a button on the first modal controller. I've tried this:

dismiss(animated: true, completion: {_ in
                self.navigateToAtmDetail(atmId:id)

            })

but as soon the current view controller is dismissed it stays dismissed and doesn't navigate to the next controller. Also we are using nibs instead of storyboard and segues so I won't have access to those. Any idea how to dismiss the current modal view controller than present a new modal view controller?

I will try to give a more through answer here. When programming in iOS all instances must be created from... other instances. When I say instance I mean an object that has been instantiated (an object that has been created in the computers memory). So, whenever you make a project with XCode, you always need to mark 'the initial view controller' . (看到右下角说“是最初的View Controller,问问自己……为什么XCode确实需要知道这一点?”) This is going to be the first instance that you create. Then it is used to spawn other instances. The reason why Apple chose this architecture is for security reasons (I think...? someone correct me if they have a better answer). You can see a very clear view of all the 'instances' of the views. You click this button while you are running a program in XCode . Then you can see the hierarchy of the views. I have made a simple program where clicking a button will load a different view. Where here I have not clicked the button and I have only loaded one view. 在我单击按钮并添加新视图之前 However here I have clicked the button and loaded the next view, 在此处输入图片说明 . At the top they both say UIWindow . That is because... (I hope you can guess this part!) They are both being instantiated from the UIWindow View. Don't believe me? Check this out! Here are the actual view hierarchies. 没有按下按钮的那个 and 按下按钮的那个 . So, in the case where you dont get what my point is. You should understand that simply by reading your question it is pretty obvious that you are trying to instantiate your model controller from another one that you are trying to dismiss. So if the model controller has been dismissed how is it supposed to instantiate another model controller? Also keep in mind that it is better to just name your controllers after their purpose like, MenuViewController, or MainController, or VideoController. The word model is usually used in MVC and it should not be used as part of a ViewControllers name. (Hope Im not sounding rude lol, I used to tutor junior high so this is how I teach XD )

Try:

let presenting = (self.presentingViewController.childViewControllers[0] as! <VIEWCONTROLLERTYPE>) //Or whatever index your controller is!
dismiss(animated: true, completion: {_ in
                presenting.navigateToAtmDetail(atmId:id)

            })

Where is the type of your presenting controller. You'll also have to move the navigateToAtmDetail method to the other controller

The short explanation (much better, detailed explanation in answers below) is that you can't present the new controller off a controller that has already been dismissed. So we want to reach back to the controller that's presenting us ( presentingViewController ), which happens to be a navigationViewController . This doesn't have the logic we want, so we need the child viewcontroller that does ( childViewControllers[x] ). Then we order that controller to do the next presentation logic. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM