swift2 迅速解除模态并推送至新VC

nwlqm0z1  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(184)

我有tableview... 1表显示一个新的模态窗口,当我按下按钮时,我想关闭模态窗口并推到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)

            })
}
du7egjpx

du7egjpx1#

您应该创建一个协议

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 Push View in Swift

aoyhnmkz

aoyhnmkz2#

更新了Swift 3和4的语法
关闭视图控制器

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

或者如果你想在一个完成块中做一些事情(在视图被关闭之后),你可以使用...

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

mbzjlibv3#

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

相关问题