【问题标题】:Conditional Segue in SwiftSwift 中的条件转场
【发布时间】:2014-07-07 15:53:39
【问题描述】:

我想在我的应用程序中创建一个“连接”按钮。当你点击它时,如果你有正确的登录,它将打开一个新页面。

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

我仍然尝试过这样的事情:

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

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

【问题讨论】:

    标签: ios uiviewcontroller swift segue


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢它的工作!你能告诉我如何通过代码触发 segue 吗?
    • 使用performSegueWithIdentifier("segueName", sender: self)
    • @mxb 采用您的方法,您是否仍会在 Storyboard 上创建(控制+拖动)segue“segueName”,从按钮到下一个场景?如果是这样,是否会在单击按钮时自动触发 segue,而不检查凭据?
    【解决方案2】:

    在 Swift 中,您可以通过以下方式继续:

    第 1 步:将一个 segue 从 ViewControllerA 插入到 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。

    【讨论】:

      猜你喜欢
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多