【问题标题】:How can I perform a segue after receive an alert answer收到警报答案后如何执行转场
【发布时间】:2017-09-22 10:15:47
【问题描述】:

今天我想尝试使应用程序的一部分在显示警报后仅在用户按下“是”警报按钮时才执行 segue。 为了更好地解释我,我希望应用程序会显示“你确定要回家吗?”它将有两个 Botton:“是”和“否”。如果用户按下 no 则没有任何反应,如果用户按下 yes 则应用程序执行 segue。问题是我不知道该怎么做。

我尝试编写一些代码行,但它不起作用。

func Alert (TITLE: String, MESSAGE: String) -> Bool()
    {
        var X = false
        let Alert = UIAlertController(title: TITLE, message: MESSAGE, preferredStyle: UIAlertControllerStyle.alert)
        Alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: { (Action) in
        X = true
        Alert.dismiss(animated: true, completion: nil)
        }))

        self.present(Alert, animated: true, completion: nil)

        Alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil))
        self.present(Alert, animated: true, completion: nil)
    return X
    }



@IBAction func ButtonAct(_ sender: Any)      //This happen if you click the botton on the screen of the iPhone
{
if Alert (TITLE: "Return to Home", MESSAGE: "Are you sure to return Home?")
{performSegue(withIdentifier: "Segue", sender: self)}
}

谢谢你的帮助

【问题讨论】:

    标签: ios iphone swift segue alert


    【解决方案1】:

    你的代码有几个问题:

    1. 您的代码中没有任何部分调用@IBAction 方法,这就是没有执行segue 的原因。警报控制器只为您提供完成处理程序;没有像 UIButton 这样的目标/动作机制。

    2. 布尔返回值在动作的完成处理程序中确定,异步。在用户有机会选择之前,您的函数会立即返回值 false。事情按以下顺序发生:

      1. 调用 alert()
      2. 函数呈现 UIAlertController
      3. 函数返回false
      4. (经过很多毫秒)
      5. 用户点击“是”
      6. 完成处理程序已执行且x 设置为true,但alert() 方法已返回false
    3. 您将显示两次警报,一次是在添加每个操作之后。您需要添加两个动作,然后只呈现一次。

    4. 在您的代码中使用标准大写。


    试试这样的:

    func alert (title: String, message: String, completion: ((Bool) -> Void)) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alertController.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action) in
            alertController.dismiss(animated: true, completion: nil)
            completion(true) // true signals "YES"
        }))
    
        alertController.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: { (action) in 
            alertController.dismiss(animated: true, completion: nil)
            completion(false) // false singals "NO"
        }))
    
        self.present(alertController, animated: true, completion: nil)
    }
    

    ...然后这样称呼它:

    alert(title: "Hi", message: "Want to proceed?", completion: { result in
        if result {
            self.performSegue(withIdentifier: "Segue", sender: self)
        }
    })
    

    【讨论】:

    • 抱歉打扰了?使用警报函数作为常见的如何处理这种情况
    • 对不起,我不明白你的问题
    • 您使用了执行转场作为直接操作,如果提问者当时出于多种目的使用了 func Alert (TITLE: String, MESSAGE: String) 将如何处理这种情况
    • 哦,那么他需要在他的Alert() 方法中添加一个closure 参数,作为完成处理程序。
    • 你好,正确,你会更新答案吗,它对未来的目的更好
    猜你喜欢
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多