斯威夫特的条件谴责

时间:2014-07-07 15:53:39

标签: ios uiviewcontroller swift segue

我想做一个" connexion"我的应用程序中的按钮当您点击它时,如果您有正确的登录信息,它将打开一个新页面。

但是,我读到我们无法撤消segue,我想我需要手动调用它。你有什么想法吗?

我还是尝试过这样的事情:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject) {
    if checkLog == true
    {
     //finish   
    }
    else
   {
    // undo
   }
}

@IBAction func ConexTAP(sender: UIButton)
{
 //check login
}

2 个答案:

答案 0 :(得分:13)

您可以在ViewController中使用以下方法执行凭据检查:

func shouldPerformSegueWithIdentifier(_ identifier: String!,
                              sender: AnyObject!) -> Bool

或者,您可以将UIButton绑定到视图控制器中的操作,在此处执行检查,然后按代码触发segue。

答案 1 :(得分:6)

在Swift中你可以继续:

步骤1:将ViewControllerA中的segue插入ViewControllerB。 segue应该从ViewControllerA本身开始,而不是从任何控件(如Button)开始。

步骤2:在故事板中为segue提供唯一标识符。例如:seageFromAtoB

第3步:在ViewControllerforA.swift中写道:

if(condition == true) {
    self.performSegueWithIdentifier("seageFromAtoB", sender: self)
}

如果你在其他一些线程中执行某些任务,那么使用performSegueWithIdentifier会抛出异常。

如果您收到此错误,则应使用:

if(condition==true){
      NSOperationQueue.mainQueue().addOperationWithBlock {
           self.performSegueWithIdentifier("seageFromAtoB", sender: self)
      }
}

这将执行具有特定条件的ViewControllerA到ViewControllerB的segue。

相关问题