如何在某些条件下取消segue(if / else)?

时间:2016-02-07 23:51:15

标签: ios swift segue

我正在创建一个应用。在某个视图中,有几个文本字段,用户在其中输入数字,然后按下按钮,并被传输到下一个视图控制器。

如果一个人点击按钮,并且一个或多个文本字段为空,segue取消,我该怎么做呢?

到目前为止,这是我的代码:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if saveButton === sender {
        let name = nameTextField.text ?? ""

        // Set the meal to be passed to MealListTableViewController after the unwind segue.
        meal = Meal(name: name)

    }

   if calcButton === sender {

    if weightInKilos.text == "" && percentOfDehydration.text == "" && ongoingLosses.text == "" && factor.text == "" {

        let alertController = UIAlertController(title: "Fields were left empty.", message:
            "You left some fields blank! Please make sure that all fields are filled in before tapping 'Calculate'.", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))

        self.presentViewController(alertController, animated: true, completion: nil)

    }

    else {
        let DestViewController: ftrViewController = segue.destinationViewController as! ftrViewController

        let weightInt: Int? = Int(weightInKilos.text!)
        let dehydrationInt: Int? = Int(percentOfDehydration.text!)
        let lossesInt: Int? = Int(ongoingLosses.text!)
        let factorInt: Int? = Int(factor.text!)

        let lrs24Int = (30 * weightInt! + 70) * factorInt! + weightInt! * dehydrationInt! * 10 + lossesInt!

        let lrsPerHourint = lrs24Int / 24

        DestViewController.lrsHr = "\(lrsPerHourint)"
        DestViewController.lrs24Hrs = "\(lrs24Int)"
    }
    }
}

1 个答案:

答案 0 :(得分:0)

在调用segue之前,您必须检查所需的一切。

因此,用户编写答案,检查一切是否正常,如果,确定,请执行segue,否则,提醒用户并要求重新输入信息。

知道了吗?