Dismiss modal, then immediately push view controller

心不动则不痛 提交于 2020-01-23 17:17:29

问题


How can I dismiss a modal and then immediately push another view?

I am adding this code in a didSelectRowAtIndexPath:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ChatTableViewController *chatVC = (ChatTableViewController*)[storyboard instantiateViewControllerWithIdentifier:@"chatVC"];
 [self dismissViewControllerAnimated:YES completion:^{
    [[self navigationController] pushViewController:chatVC animated:YES];
}];

The modal view dismisses, but nothing happens after. Any ideas?


回答1:


You can't push a view controller into a view dismissed, if it's dismissed it disappears so it's not logical to push another view, cause the parent view controller is deleted. Probably you have this: - ViewController 1 --> Modal ViewController 2 -->Wanted to dismiss VC2 and push VC3

What you have to do is - ViewController 1 --> Modal ViewController 2 --> Dismiss VC2 --> Push VC3 on VC1

You can do it with notifications. The most efficient way is to use delegates, create a delegate on VC2 that notifies V1 when it dismisses and then just push VC3.




回答2:


No, it cant be done that quickly as far as I know, you need to add a delay betwwn the dissmiss and the navigation of another controller, only then will it execute else it would crash an error saying, trying to push a controller while dismissing other




回答3:


This answer tries to solve the original problem asked above.

A,B,C viewControllers. A is the root VC of a navigationController.

A-> modally_presents -> B-> dismissing_modally_prsented_B_and_automatically_pushing -> C

Note: All presentation and dismissals are done using segues. I have marked important sequence of actions.

class CViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

/************************************************************/

protocol BDelegate: class {
    func BDelegateFunc(someRandomString: String)
}

class BViewController: UIViewController {

    weak var delegate: BDelegate?
    var name: String?

    override func viewDidLoad() {
        super.viewDidLoad()
        name = "swappy"
    }

    @IBAction func btnClicked(_ sender: Any) {
       // 3 DISMISS using segue
        performSegue(withIdentifier: "unwindToAViewController", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // 4
        print(segue.identifier)
    }

    deinit {
        // 6
        print(String(describing: type(of: self)) + #function)
        delegate?.BDelegateFunc(someRandomString: name!)
    }
}

/************************************************************/

class AViewController: UIViewController, BDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func btnClicked(_ sender: Any) {
        // 1 MODAL 
        performSegue(withIdentifier: "presentBFromA", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "presentBFromA" {
            // 2
            let vc = segue.destination as! BViewController
            vc.delegate = self
        } else if segue.identifier == "showCFromA" {
           // 8 This will finally push C in navigationController
            print(segue.identifier)
        }
    }

    @IBAction func unwindToAViewController(segue: UIStoryboardSegue) {
       // 5
        print(#function)
    }

    func BDelegateFunc(someRandomString: String) {
        // 7
        print(someRandomString)
        performSegue(withIdentifier: "showCFromA", sender: self)
    }
}


来源:https://stackoverflow.com/questions/24939465/dismiss-modal-then-immediately-push-view-controller

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