【问题标题】:How to do a modal segue and pop to root in navigation controller simultaneously?如何同时在导航控制器中进行模态搜索和弹出到根目录?
【发布时间】:2018-06-25 19:13:10
【问题描述】:

我想跳到窗帘后面的根部,同时以模态方式呈现一个新的视图控制器。当用户使用完这个新的视图控制器后,它将被关闭,并且导航控制器的根视图控制器将在它后面。

我尝试过使用

self.navigationController?.popToRootViewController(animated: false)

prepareForSegue: 方法内部,但它在模态视图控制器完全呈现之前显示根视图控制器(因此向用户显示幕后发生的事情,这不是一个理想的功能)。

如果尝试弹出模态显示的视图控制器的viewDidLoad:,则什么都不会发生。

解决这个问题的最佳方法是什么?

【问题讨论】:

    标签: ios iphone swift uinavigationcontroller modalviewcontroller


    【解决方案1】:

    扭转局面,同时解除模态加popToRootViewController

    不确定您是否可以使用 StoryBoards 做到这一点(因为我更喜欢 XIB),所以您可能需要更多代码。

    【讨论】:

    • 这将导致Unbalanced calls to begin/end appearance transitions for <Pushed VC here>,希望这只是一个警告,不会导致任何问题。但它有效。
    • 我没有使用这个答案,只是添加了我所知道的。我最终手动操作了视图控制器数组。
    【解决方案2】:

    有很多不同的方法可以实现这一目标。这将通过委托在后台将 FirstViewController 从堆栈中弹出。

    class FirstViewController: UIViewController {
    
        @IBOutlet weak var presentModal: UIBarButtonItem!
    
        @IBAction func action(_ sender: Any) {
            self.performSegue(withIdentifier: "presentModal", sender: nil)
    
        }
    
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "presentModal" {
                let destVC = segue.destination as! UINavigationController
                let topVC = destVC.topViewController as! SecondViewController
                topVC.delegate = self
            }
        }
    
    
    }
    
    extension FirstViewController: SecondViewControllerDelegate {
    
        func didDismissModal(_ controller: SecondViewController) {
            self.navigationController?.popViewController(animated: false)
        }
    
    }
    
    protocol SecondViewControllerDelegate {
        func didDismissModal(_ controller: SecondViewController)
    }
    
    class SecondViewController: UIViewController {
    
        var delegate: SecondViewControllerDelegate?
    
        @IBAction func action(_ sender: Any) {
             self.delegate?.didDismissModal(self)
             self.dismiss(animated: true, completion: nil)
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      相关资源
      最近更新 更多