仅在条件为TRUE的情况下,在performegue和应该执行segue之间使用什么来执行segue

时间:2018-10-23 03:05:48

标签: ios swift segue

我在解决有关segue的问题时遇到了困难。场景应该是当用户点击backbutton时,它将提示一个警报,询问您您确定要离开此页面吗? LeaveStay是选项。如果用户点击Leave响应,它将返回到DashBoard,如果用户点击Stay,它将仅保留其原始ViewController。问题是segue会自动执行而不会检查backbutton内部的条件。我是新手,遇到的所有问题对我来说都是新的。如何正确构造代码以满足backbutton的要求。在执行顺序之前,按钮将如何首先执行条件?希望您能够帮助我。以下是我的代码供您参考。在这里待了将近1周。谢谢。

执行Segue

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDashBoard" {
        if let dashBoardVC = segue.destination as? DashBoardViewController {
            dashBoardVC.validPincode = validPincode
            dashBoardVC.participants = participants
            dashBoardVC.event = event
        }
    }
}

后退按钮

 @IBAction func backbutton(_ sender: UIButton) {

    let alert = SCLAlertView(appearance: confirmationAppearance)
   _ = alert.addButton("Leave", action: {
        self.dismiss(animated: true, completion: nil)
    self.performSegue(withIdentifier: "showDashBoard", sender: sender)
    })
    _ = alert.addButton("Stay", action: { })
    _ = alert.showError("Confirmation", subTitle: "Are you sure you want to leave this page?")

    }

尝试使用shouldPerformSegue,但我不知道如何开始

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if identifier ==  "showDashBoard" {

        var segueShouldOccur: Bool!

        if segueShouldOccur == false {
            print("nope, user wants to stay")
            return false
        }
        else {
            self.performSegue(withIdentifier: "showDashBoard", sender: sender)
            print("Yep, the user wants to leave")
        }
    }


    return true
}

用于情节提要的屏幕截图 back button

1 个答案:

答案 0 :(得分:0)

为了像我这样的人的利益,新手请使用swift。我解决了自己的问题。我省略了back buttonDashBoard之间的连接。我从ParticipantsViewControllerDashBoardViewController转移了segue::) segue

@IBAction func backbutton(_ sender: UIButton) {
    let alert = SCLAlertView(appearance: confirmationAppearance)
    _ = alert.addButton("Leave", action: {
        self.dismiss(animated: true, completion: {
            self.performSegue(withIdentifier: "showDashBoard", sender: sender)
        })
    })

    _ = alert.addButton("Stay", action: { })
    _ = alert.showError("Confirmation", subTitle: "Are you sure you want to leave this page?")
}