为什么不是';t我的用户警报代码在Swift中按预期工作

为什么不是';t我的用户警报代码在Swift中按预期工作,swift,Swift,我的应用程序中有一个重置按钮。我对其进行了编码,这样如果用户点击此按钮重置应用程序,他们会首先收到用户警报。此警报提供信息并提供两种选择:(1)取消,(2)继续 这个想法是,如果他们点击“取消”,游戏将不会重置。但如果他们点击Continue,游戏将重置 第1期 目前,当用户点击重置按钮时,游戏会在警报显示之前立即重置。警报显示后,如果他们点击“取消”,则不会再发生任何事情,但如果他们点击“继续”,则游戏将再次重置 因此,从表面上看,警报按钮可以正常工作——只是点击重置图标已经在警报出现之前执行

我的应用程序中有一个重置按钮。我对其进行了编码,这样如果用户点击此按钮重置应用程序,他们会首先收到用户警报。此警报提供信息并提供两种选择:(1)取消,(2)继续

这个想法是,如果他们点击“取消”,游戏将不会重置。但如果他们点击Continue,游戏将重置

第1期

目前,当用户点击重置按钮时,游戏会在警报显示之前立即重置。警报显示后,如果他们点击“取消”,则不会再发生任何事情,但如果他们点击“继续”,则游戏将再次重置

因此,从表面上看,警报按钮可以正常工作——只是点击重置图标已经在警报出现之前执行了重置操作

第二期

我的警报代码适用于iOS 8及以上用户以及iOS 8之前的用户。我如何修改这部分代码,使iOS 8之前的用户也能够“取消”或“继续”?目前,他们只有一个“继续”选项

我的警报代码如下:

@IBAction func buttonResetGame(sender: UIButton) {

    if #available(iOS 8.0, *) {
        let alert = UIAlertController(title: "Info", message: "This will reset your score and questions.", preferredStyle: UIAlertControllerStyle.Alert)

        // add the Continue button and action
        alert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.Destructive, handler: {action in

            // do this...
            self.ResetGame()
        }))

        // add the Cancel button and action
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

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


    }else{
        // For pre-iOS 8 users
        let alert = UIAlertView()
        alert.title = "Info"
        alert.message = "This will reset your score and questions."
        alert.addButtonWithTitle("Continue")
        alert.show()
    }
    ResetGame()

}
func ResetGame() {
    PlaySoundReset()
    score = 0
    totalquestionsasked = 0
    SaveScore()
    LoadScore()
}
如果相关,我重置游戏的代码如下:

@IBAction func buttonResetGame(sender: UIButton) {

    if #available(iOS 8.0, *) {
        let alert = UIAlertController(title: "Info", message: "This will reset your score and questions.", preferredStyle: UIAlertControllerStyle.Alert)

        // add the Continue button and action
        alert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.Destructive, handler: {action in

            // do this...
            self.ResetGame()
        }))

        // add the Cancel button and action
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

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


    }else{
        // For pre-iOS 8 users
        let alert = UIAlertView()
        alert.title = "Info"
        alert.message = "This will reset your score and questions."
        alert.addButtonWithTitle("Continue")
        alert.show()
    }
    ResetGame()

}
func ResetGame() {
    PlaySoundReset()
    score = 0
    totalquestionsasked = 0
    SaveScore()
    LoadScore()
}
在if/else之后删除额外的ResetGame()调用:

@IBAction func buttonResetGame(sender: UIButton) {

    if #available(iOS 8.0, *) {
        let alert = UIAlertController(title: "Info", message: "This will reset your score and questions.", preferredStyle: UIAlertControllerStyle.Alert)

        // add the Continue button and action
        alert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.Destructive, handler: {action in

            // do this...
            self.ResetGame()
        }))

        // add the Cancel button and action
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

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


    }else{
        // For pre-iOS 8 users
        let alert = UIAlertView()
        alert.title = "Info"
        alert.message = "This will reset your score and questions."
        alert.addButtonWithTitle("Continue")
        alert.show()
    }
    ResetGame() //remove this line ? You are calling the RestGame func...

}
对于问题的第二部分,您需要将警报视图的委托设置为“self”或其他形式,然后实现UIAlertViewDelegate方法:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 9_0);

按钮索引将为您提供每个按钮,即0和1。

else
块之后有一个
ResetGame()
,该块总是被执行-您期望什么?哦!我知道我不应该这么晚才做这件事,这太明显了,太尴尬了!但奇怪的是,为什么会立即发生(即在警报出现之前)?