转到没有segues的第二个viewController

fs_*_*gre 1 delegates ios segue swift

在下面的代码中我有一个动作/按钮,当点击时带你到另一个viewController(SecondViewController),一切正常,但有一行我不完全理解的代码.

这行代码在做什么?

secondVC.delegate = self

我们在这里谈论的代表是什么?如果我只需要转到其他视图并且我没有传递任何数据,那真的需要吗?

@IBAction func goToView(sender: AnyObject) {  
    let secondVC= storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as! SecondViewController  
    secondVC.delegate = self // is this needed?  
    self.presentViewController(secondVC, animated:true, completion:nil)  
} 
Run Code Online (Sandbox Code Playgroud)

Cha*_*har 5

  1. secondVC.delegate = self

这行编码是将当前类的对象引用传递给SecondViewController.现在,您可以通过在secondViewController中使用委托对象来调用firstViewController的方法(我假设此名称).

  1. 不需要此代表

,如果你只是想转到你的情况下是NextViewController的Ne​​xt Screen.

  1. 有用的代码:

下面将把你传递给下一个控制器,确保你有没有navigationController.与NavigationController一样,您必须将pushConController转换为堆栈.

@IBAction func goToView(sender: AnyObject) {  
    let secondVC= storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as! SecondViewController  
    self.presentViewController(secondVC, animated:true, completion:nil)
} 
Run Code Online (Sandbox Code Playgroud)

或者在NavigationController的情况下

@IBAction func goToView(sender: AnyObject) {  

let secondVC = self.storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as? SecondViewController
    self.navigationController?.pushViewController(secondVC!, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

我希望我的描述性答案会对你有所帮助.随意在评论中询问任何查询.

谢谢.