Ios 转到第二个不带分段的viewController

Ios 转到第二个不带分段的viewController,ios,swift,delegates,segue,Ios,Swift,Delegates,Segue,在下面的代码中,我有一个操作/按钮,当点击它时,它会将您带到另一个viewController(SecondViewController),一切正常,但有一行代码我不完全理解 这行代码在做什么 secondVC.delegate=self 我们在这里谈论的是什么代表?如果我只需要转到其他视图而不传递任何数据,那么真的需要这样做吗 @IBAction func goToView(sender: AnyObject) { let secondVC= storyboard?.instant

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

如果我处在你的处境,我只会使用以下代码:

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

如果我处在你的处境,我只会使用以下代码:

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

secondVC.delegate=self

并清楚地使用它

@IBAction func goToView(sender: AnyObject) {  
    let secondVC =  storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as! SecondViewController   
    self.presentViewController(secondVC, animated:true, completion:nil)  
} 
不需要使用代理vc。 谢谢

删除

secondVC.delegate=self

并清楚地使用它

@IBAction func goToView(sender: AnyObject) {  
    let secondVC =  storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as! SecondViewController   
    self.presentViewController(secondVC, animated:true, completion:nil)  
} 
不需要使用代理vc。 谢谢

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

  • 不需要此委托
  • ,如果您只是想转到下一个屏幕,即您的案例中的SecondViewController

  • 有用代码:
  • 以下内容将传递给下一个控制器,请确保您是否拥有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)
    }
    
    我希望我的描述性回答能对你有所帮助。欢迎在评论中提出任何疑问

    谢谢

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

  • 不需要此委托
  • ,如果您只是想转到下一个屏幕,即您的案例中的SecondViewController

  • 有用代码:
  • 以下内容将传递给下一个控制器,请确保您是否拥有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)
    }
    
    我希望我的描述性回答能对你有所帮助。欢迎在评论中提出任何疑问



    谢谢。

    您是否在second view controller中创建了自定义委托?只需移动到second view controller,不需要代码行。如果您已经在类中实现了委托方法,并且需要从secondViewController调用,那么您需要设置presentViewController中必须有应该从secondViewController调用的协议。如果不使用协议,禁用secondVC.delegate=self不会造成伤害,如果我理解正确,这仅在使用委派模式传递数据时才需要。您是否在second view controller中创建了自定义委派?只是为了移动到second view controller,不需要代码行。如果您已经在类中实现了委托方法,并且需要从secondViewController调用,那么您需要设置presentViewController中必须有应该从secondViewController调用的协议。如果不使用协议,禁用secondVC.delegate=self不会造成伤害,如果我理解正确,这只在使用委托模式传递数据时才需要。这使用segues,我不想在这种情况下使用segues。谢谢。这是用segues,我不想在这个案子上用segues。谢谢。@Veer Suthar-明白了。现在,让我确保我理解了这一行
    IF
    ,它与委托模式一起用于传递数据。用通俗易懂的英语说,这一行代码在与“委派”一起使用时表示“我将是第二个ViewController的委派”对吗?委派,我们派往其他国家,代表我们的国家,同样,委派意味着传递您自己的对象,或者重新输入其他控制器,以便他可以代表您并调用您的方法和变量@fs_tigreLike美国代表团(国家),在联合国办事处(组织),所以在这里,无论该代表团在联合国办事处中做什么,都将是美国(国家)的言行。在这里,FirstViewController是美国,而SecondViewController是UNO,在SecondViewController内部,您可以使用该代理处理FirstViewController的方法。是的,准确地说,删除=FirstViewController的对象引用亲爱的。欢迎您@fs_tigre@Veer Suthar-明白了。现在,让我确保我理解了这一行
    IF
    ,它与委托模式一起用于传递数据。用通俗易懂的英语说,这一行代码在与“委派”一起使用时表示“我将是第二个ViewController的委派”对吗?委派,我们派往其他国家,代表我们的国家,同样,委派意味着传递您自己的对象,或者重新输入其他控制器,以便他可以代表您并调用您的方法和变量@fs_tigreLike美国代表团(国家),在联合国办事处(组织),所以在这里,无论该代表团在联合国办事处中做什么,都将是美国(国家)的言行。在这里,FirstViewController是美国,而SecondViewController是UNO,在SecondViewController内部,您可以使用该代理处理FirstViewController的方法。是的,准确地说,删除=FirstViewController的对象引用亲爱的。欢迎您@弗斯努蒂格雷酒店