Swift:如果条件不满足,则保持segue不执行

时间:2016-05-12 03:04:59

标签: ios swift segue

目前我在我的应用程序中处理两个不同的ViewControllers,我们可以将它们称为VC1和VC2。 VC1显示一个注册表单,在UITableView的每一行中都有一个问题,并有一个编辑按钮。编辑按钮是VC1和VC2的segue。 VC2显示一个允许用户编辑问题的界面。我想编写应用程序,以便除非您选择了要编辑的问题(行),否则不能从VC1转到VC2。现在我有一些设置,但我有可能倒退。

在VC1中:

// this method isn't called until VC2 segues back to VC1... so I don't know how
// to make sure that a selection has been made before pressing the button
@IBAction func editQuestion(segue:UIStoryboardSegue) {
        if let editQuestion = segue.sourceViewController as? VC2 {

        }
}

在VC2中:

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

    // check to make sure that user has completed all fields of question
    // if they haven't completed all fields, return false (do not segue)
    // else segue to VC1
}

1 个答案:

答案 0 :(得分:4)

在VC1中实施:

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
        if identifier == <your storyboard segue identifier> {

        }
    } 

给你的storyboard segue一个标识符,然后你可以在VC1中进行检查,如果你不想发生segue,则返回false。