简体   繁体   English

3种视图控制器转换方式

[英]3 ways of view controller transition

i am a beginner in iOS development, and recently, i just follow along the tutorial for beginners. 我是iOS开发的初学者,最近,我只是按照初学者教程学习。

let say i want to move from one VC to another VC by clicking a button, so i just find out that there are three ways to move from one ViewController to another ViewController (modal segue). 假设我想通过单击按钮从一个VC移到另一个VC,所以我只是发现有三种方法可以从一个ViewController移到另一个ViewController(模态选择)。

  1. in main storyboard, i just click control and drag from the button to th destination view controller and choose present modally 在主情节提要中,我只需单击“控制”,然后将其从按钮拖动到目标视图控制器,然后以模态形式选择“当前”

  2. programmaticaly, by implementing the code below 通过实现以下代码以编程方式

     @IBAction func logInButtonDidPressed(_ sender: Any) { // modal transition to VC2 let viewController2 = storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2 present(viewController2, animated: true, completion: nil) } 
  3. programatically,by using perform segue function 以编程方式,通过执行执行功能

     @IBAction func logInButtonDidPressed(_ sender: Any) { performSegue(withIdentifier: "toSecondViewController", sender: self) } 

are they just the same ? 他们是一样的吗? or is it used for different cases? 还是用于不同的情况?

Thanks in advance :) 提前致谢 :)

I would use segues, as there are some advantages compared to manual presentation: 我将使用segues,因为与手动演示相比有一些优势:

  • You can create unwind segues to exit the current view controller to any view controller in the hierarchy. 您可以创建展开序列以将当前视图控制器退出到层次结构中的任何视图控制器。

  • You can add 3D touch support to segues with one mouse click. 您可以通过单击一下鼠标来添加3D触摸支持来进行搜索。

The first and last method produce identical results. 第一种和最后一种方法产生相同的结果。 I would create segues with clicking and dragging whenever possible. 我会尽可能通过单击和拖动来创建segue。 If you need to do some data validation or other stuff before performing a transition, you have to call the performSegue method manually. 如果需要在执行过渡之前进行一些数据验证或其他工作,则必须手动调用performSegue方法。

Yes, they are similar. 是的,它们是相似的。 And the obvious difference I think is the data passing. 我认为明显的区别是数据传递。 The first and third one are same, use the following method to pass data to next controller: 第一个和第三个相同,请使用以下方法将数据传递到下一个控制器:

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    if let viewController2 = segue.destination as? ViewController2 {
        viewController2.someProperty = someValue
    }
}

For second transition, you directly set the data when creating the next controller: 对于第二次转换,您可以在创建下一个控制器时直接设置数据:

let viewController2 = storyboard?.instantiateViewController(withIdentifier: 
"ViewController2") as! ViewController2

viewController2.someProperty = someValue

present(viewController2, animated: true, completion: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM