【问题标题】:swift dismiss modal and push to new VC快速解除模式并推送到新的 VC
【发布时间】:2015-11-19 14:24:36
【问题描述】:

我有 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)

            })
}

【问题讨论】:

    标签: swift swift2 push ios9 modalviewcontroller


    【解决方案1】:

    你应该创建一个协议

    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

    【讨论】:

    • 哇!太感谢了。我将您的项目应用于我的问题并解决了它!我喜欢它。
    • 对于我们新手 - “创建协议” - 在哪里?
    • @Donnelle 查看演示
    • 我爱你。我不开玩笑,只是花了大约 8 个小时来尝试学习 Swift 的这一部分并在解决我在应用程序中遇到的问题时理解它。你刚刚用你的演示解决了我的问题。你是英雄。
    【解决方案2】:

    更新了 Swift 3 和 4 的语法

    关闭视图控制器

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

    或者如果您想在完成块中做某事(在视图被关闭后),您可以使用...

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 不,这不起作用。它将关闭并且没有推送发生
      • 是的。这也行不通,对不起。但是,委托解决方案应该可以工作......
      • 我把协议和穿var delegate + 方法放在Modal window VC中。我应该把 pushViewController 放在哪里?
      • 在显示模态的VC中。该 VC 需要实现委托,然后在显示之前在模态 VC 中将自己设置为委托。
      • 好吧,我现在迷路了....我有这样的设置:1) FavouriteVC(TableView-embed in navigation controller) -> 2) CreateProfileVC(Modal window) -> 3) RegistrationVC (final查看导航控制器)我的问题是......如何设置您的解决方案?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 2020-06-03
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多