swift dismiss modal and push to new VC

邮差的信 提交于 2019-12-22 12:54:30

问题


I have tableview... 1 table showing a new modal window and when I press the button I want to dismiss the modal window and push to VC. My code only hide the modal view but no push is made.

    @IBAction func registrationBtn(sender: AnyObject) {

    let openNewVC = self.storyboard?.instantiateViewControllerWithIdentifier("registrationVcID") as! RegistrationVC

    self.dismissViewControllerAnimated(false, completion: { () -> Void   in
         self.navigationController?.pushViewController(openNewVC, animated: true)

            })
}

回答1:


You should create a protocol

protocol View1Delegate: class {
    func dismissViewController(controller: UIViewController)
}

When you tap button on Register will call delegate back to TableView. TableViewController should implement:

  func dismissViewController(controller: UIViewController) {
    controller.dismissViewControllerAnimated(true) { () -> Void in
        //Perform segue or push some view with your code

    }
}

You can do anything in here. Push screen you want. Detail implement you can see my demo: Demo Push View in Swift




回答2:


Updated syntax for Swift 3 and 4

Dismissing view controllers

self.dismiss(animated: true, completion: nil)

or if you'd like to do something in a completion block (After the view has been dismissed) you can use...

self.dismiss(animated: true, completion: {
            // Your code here
        })



回答3:


self.navigationController won't do anything inside that completion block as it's already dismissed.

Instead create a delegate that you call on the parent view controller when dismissing the current one.

protocol PushViewControllerDelegate: class {
    func pushViewController(vc: UIViewController)
}

and then in your closing VC you can store delegate and call it in the completion block.

weak var delegate: PushViewControllerDelegate?

self.dismissViewControllerAnimated(false, completion: { () -> Void   in

    let openNewVC = self.storyboard?.instantiateViewControllerWithIdentifier("registrationVcID") as! RegistrationVC
    self.delegate?.pushViewController(openNewVC)
}

And the implementation in your presenting view controller

//declaration
class OneController: UIViewController, PushViewControllerDelegate

//show modal
let twoController = TwoController()
twoController.delegate = self
self.presentViewController(twoController, animated:true)

//MARK: PushViewControllerDelegate

func pushViewController(vc: UIViewController) {
    self.navigationController?.push(vc, animated:true)
}


来源:https://stackoverflow.com/questions/33806577/swift-dismiss-modal-and-push-to-new-vc

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