swift 关闭模态并推送到新的 VC
swift dismiss modal and push to new VC
我有 table视图... 1 table 显示新模态 window 当我按下按钮时我想关闭模态 window 并按下至 VC。我的代码只隐藏模态视图但没有推送。
@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)
})
}
self.navigationController 不会在该完成块内执行任何操作,因为它已被关闭。
而是创建一个委托,您在关闭当前视图控制器时在父视图控制器上调用该委托。
protocol PushViewControllerDelegate: class {
func pushViewController(vc: UIViewController)
}
然后在结束时 VC 您可以存储委托并在完成块中调用它。
weak var delegate: PushViewControllerDelegate?
self.dismissViewControllerAnimated(false, completion: { () -> Void in
let openNewVC = self.storyboard?.instantiateViewControllerWithIdentifier("registrationVcID") as! RegistrationVC
self.delegate?.pushViewController(openNewVC)
}
并在您的呈现视图控制器
中实现
//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)
}
你应该创建一个协议
protocol View1Delegate: class {
func dismissViewController(controller: UIViewController)
}
当您点击注册按钮时,会将委托回调回 TableView。 TableViewController 应该实现:
func dismissViewController(controller: UIViewController) {
controller.dismissViewControllerAnimated(true) { () -> Void in
//Perform segue or push some view with your code
}
}
你可以在这里做任何事情。推你想要的屏幕。详细实现可以看我的demo:Demo Push View in Swift
更新了 Swift 3 和 4
的语法
正在关闭视图控制器
self.dismiss(animated: true, completion: nil)
或者,如果您想在完成块中执行某些操作(在关闭视图后),您可以使用...
self.dismiss(animated: true, completion: {
// Your code here
})
我有 table视图... 1 table 显示新模态 window 当我按下按钮时我想关闭模态 window 并按下至 VC。我的代码只隐藏模态视图但没有推送。
@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)
})
}
self.navigationController 不会在该完成块内执行任何操作,因为它已被关闭。
而是创建一个委托,您在关闭当前视图控制器时在父视图控制器上调用该委托。
protocol PushViewControllerDelegate: class {
func pushViewController(vc: UIViewController)
}
然后在结束时 VC 您可以存储委托并在完成块中调用它。
weak var delegate: PushViewControllerDelegate?
self.dismissViewControllerAnimated(false, completion: { () -> Void in
let openNewVC = self.storyboard?.instantiateViewControllerWithIdentifier("registrationVcID") as! RegistrationVC
self.delegate?.pushViewController(openNewVC)
}
并在您的呈现视图控制器
中实现//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)
}
你应该创建一个协议
protocol View1Delegate: class {
func dismissViewController(controller: UIViewController)
}
当您点击注册按钮时,会将委托回调回 TableView。 TableViewController 应该实现:
func dismissViewController(controller: UIViewController) {
controller.dismissViewControllerAnimated(true) { () -> Void in
//Perform segue or push some view with your code
}
}
你可以在这里做任何事情。推你想要的屏幕。详细实现可以看我的demo:Demo Push View in Swift
更新了 Swift 3 和 4
的语法正在关闭视图控制器
self.dismiss(animated: true, completion: nil)
或者,如果您想在完成块中执行某些操作(在关闭视图后),您可以使用...
self.dismiss(animated: true, completion: {
// Your code here
})