【问题标题】:Go to second viewController without segues转到没有 segues 的第二个 viewController
【发布时间】:2016-08-22 12:10:51
【问题描述】:

在下面的代码中,我有一个操作/按钮,当点击它时会将您带到不同的 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)  
} 

【问题讨论】:

  • 您是否在第二个视图控制器中创建了自定义委托?
  • 只是移动到 SecondViewController 那行代码不是必需的。如果你已经在你的类中实现了委托方法并且需要从 secondViewController 调用,那么你需要设置
  • presentViewController 中必须有应该从 secondViewController 调用的协议。禁用 secondVC.delegate = 如果不使用协议,则自身不会受到伤害
  • 有道理,如果我理解正确的话,只有在使用委托模式来传递数据时才需要这样做。

标签: ios swift delegates segue


【解决方案1】:
  1. secondVC.delegate = self

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

  1. 不需要此委托

,如果您只是想转到 Next Screen,在您的情况下是 SecondViewController。

  1. 有用的代码:

以下会将您传递到下一个控制器,请确保您有 navigationController 或没有。与 NavigationController 一样,您必须将 ViewController 推入堆栈。

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

或者在 NavigationController 的情况下

@IBAction func goToView(sender: AnyObject) {  

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

希望我的描述性回答对您有所帮助。随时在 cmets 中询问任何问题。

谢谢。

【讨论】:

  • @Veer Suthar - 明白了。现在,让我确保我理解这一行 IF 与委托模式一起使用以传递数据。用简单的英语,当与委托一起使用时,说这行代码是 I will be the delegate of the secondViewController 是否正确?
  • 委托,我们发送到其他国家,代表我们的国家,同样,委托意味着将您自己的对象或代表传递给其他控制器,以便他可以代表您并调用您的方法和变量。 @fs_tigre
  • 就像美国(国家)代表团在 UNO(组织)中一样,所以在这里,无论该代表在 UNO 中做什么,都是美国(国家)的言行。在这里,FirstViewController 是 USA,而 SecondViewController 是 UNO,在 secondViewController 中,您可以使用该委托处理 FirstViewControllers 的方法。
  • 是的,没错,delete = 亲爱的 firstviewcontroller 的对象引用。
  • 欢迎您! @fs_tigre
【解决方案2】:

删除

secondVC.delegate = self

并清楚地使用它;

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

不需要使用委托 vc。 谢谢

【讨论】:

    【解决方案3】:

    如果我遇到你的情况,我会使用以下代码:

    @IBAction func goToView(sender: AnyObject)
    {
      self.performSegueWithIdentifier("secondViewController", sender: sender)
    }
    

    【讨论】:

    • 这使用了segues,我不想在这种情况下使用segues。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    相关资源
    最近更新 更多