【问题标题】:Conditional Segue while passing data?传递数据时的条件Segue?
【发布时间】:2017-02-25 03:57:21
【问题描述】:

只有在满足我的条件时,我才会尝试从 viewcontroller 转到 2ndviewcontroller。我已将视图控制器中按钮的 segue 连接到 2ndviewcontroller。到目前为止,我有:

@IBAction func switchView(_ sender: AnyObject) {
        // if a and b's textfields aren't empty
        if(!(a.text?.isEmpty)! && !(b.text?.isEmpty)!) {
            a1 = a.text!;
            b1 = b.text!;
        }else{
            // do something
        }

    }

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if(identifier == "2ndviewcontroller") {
        if(!(a.text?.isEmpty)! && !(b.text?.isEmpty)!)  {
            return true
        }
    }
    return false
}

有了这个,我只能在 a 和 b 的文本字段不为空时进行转场。这正是我想要的,但我也想将数据从 viewcontroller 传递到 2ndviewcontroller。

func prepForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "2ndviewcontroller" {
         let 2ndviewcontroller = segue.destination as! 2ndviewcontroller
            2ndviewcontroller.c = a

        }
    }
}

我使用上面的代码将数据从 viewcontroller 传递到 2ndviewcontroller。问题是我不知道如何将它们结合起来传递数据,并且只有在满足条件时才进行 segue。当我拥有这两个函数时,布尔函数正确执行,但 prepForSegue 不传递数据。当我注释掉 bool 函数时,prepForSegue 会传递数据,但我无法应用条件来进行 segue。

编辑:使用以下 cmets 中提供的 prepareForSegue 方法修复。

【问题讨论】:

  • 你为什么要把它们结合起来?它们是两种不同的方法。一旦决定是否执行 segue,另一个允许您根据需要传递数据。如果 segue 不应该执行,你会传递什么数据?
  • 当我拥有这两个函数时,布尔函数会正确执行,并在满足条件时进行转场。但是,传递数据的功能不起作用。当我注释掉布尔函数时,我可以很好地传递数据,但是当然,无论是否满足条件,只要单击按钮,我都会进行转场。我已经更新了我的问题以澄清。
  • 两者都应该被执行。正确的方法名称是prepareForSegue,而不是prepForSegue
  • 你是对的。 PrepForSegue 是我自己编写的一个方法,但是在使用 xCode 的自动填充切换到 prepareForSegue 后,这两个方法都可以正确执行。谢谢!
  • 作为答案添加。

标签: ios uiviewcontroller uistoryboardsegue


【解决方案1】:

如 cmets 中所述,方法名称应为 prepareForSegue,而不是 prepForSegue

【讨论】:

    猜你喜欢
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多