swift dismiss modal and push to new VC

戏子无情 提交于 2019-12-06 06:22:06

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

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

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