传递数据时有条件的猜测?

时间:2016-10-15 22:20:14

标签: ios uiviewcontroller uistoryboardsegue

我只是在满足条件时才尝试将segue从viewcontroller转到2ndviewcontroller。我已经将viewcontroller中的按钮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的文本字段不为空的情况下才能生成segue。这正是我想要的,但我也想将数据从viewcontroller传递到2ndviewcontroller。

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

        }
    }
}

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

编辑:使用下面评论中提供的prepareForSegue方法修复。

1 个答案:

答案 0 :(得分:2)

正如评论中所讨论的,方法名称应为prepareForSegue,而不是prepForSegue

相关问题