ios - 使用 UIAlertView 取消触摸

标签 ios swift uialertcontroller

我正在开发一款游戏。

此游戏有一个 UITableViewController,用作设置和选项功能。

您可以选择的选项之一是将对手从传球和打球切换为 AI(或相反)。

我想做的是使用 UIAlertController 拦截触摸,检查正在进行的游戏,并提示用户取消更改或继续。

我已经在 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 函数中实现了执行此操作的代码。

我的代码如下:

if indexPath.section == 1 {

    // Here we need to detect a game in progress and ask for a confirmation before switching to another mode.
    if getGameInProgress() == true {

        // Display choice dialog
        alert = UIAlertController (title: alertCaption, message: alertMessage, preferredStyle: .Alert)

        let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(action: UIAlertAction) -> Void in
            self.alert.dismissViewControllerAnimated(true, completion: { _ in })
            return
        })
        alert.addAction(cancelAction)

        let ok: UIAlertAction = UIAlertAction(title: "Confirm", style: .Default, handler: {(action: UIAlertAction) -> Void in
            self.alert.dismissViewControllerAnimated(true, completion: { _ in })
            // Do nothing, just let the touch method continue...
        })
        alert.addAction(ok)

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


    if indexPath.row == 0 {
        // "Pass and Play"
        setOpponentPreference("Pass and Play")
    }

    if indexPath.row == 1 {
        // "Virtual Robert"
        setOpponentPreference("Virtual Robert")
    }

    if indexPath.row == 2 {
        // "Virtual Nathan"
        setOpponentPreference("Virtual Nathan")
    }

    // No matter which difficulty selected:
    gameboard.setDifficulty()
    // Create a method to reset the game, but without fading out the menu screen (the current new game method takes you directly to the game. The player may not want that in this instance.)
}

我遇到的问题是警报 Controller 显示并等待输入,但它不会阻止 didSelectRowAtIndexPath 函数形式的其余部分继续。

如何在处理单元格上的触摸之前接收用户输入?

最佳答案

您好,您只需将代码放入“确认”选项的完成部分,只有当用户点击 UIAlertViewController 的“确认”按钮时,您的代码才会执行​​。

self.alert.dismissViewControllerAnimated(true, completion: { _ in })
                        // Do nothing, just let the touch method continue...
                    })

所以你的代码必须是

if indexPath.section == 1 {

    // Here we need to detect a game in progress and ask for a confirmation before switching to another mode.
    if getGameInProgress() == true {

        // Display choice dialog
        alert = UIAlertController (title: alertCaption, message: alertMessage, preferredStyle: .Alert)

        let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(action: UIAlertAction) -> Void in
            self.alert.dismissViewControllerAnimated(true, completion: { _ in })
            return
        })
        alert.addAction(cancelAction)

        let ok: UIAlertAction = UIAlertAction(title: "Confirm", style: .Default, handler: {(action: UIAlertAction) -> Void in
            self.alert.dismissViewControllerAnimated(true, completion: { _ in })

      if indexPath.row == 0 {
        // "Pass and Play"
        setOpponentPreference("Pass and Play")
        }

     if indexPath.row == 1 {
        // "Virtual Robert"
        setOpponentPreference("Virtual Robert")
      }

    if indexPath.row == 2 {
        // "Virtual Nathan"
        setOpponentPreference("Virtual Nathan")
    }

    // No matter which difficulty selected:
    gameboard.setDifficulty()
    // Create a method to reset the game, but without fading out the menu screen (the current new game method takes you directly to the game. The player may not want that in this instance.)
        })
        alert.addAction(ok)

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

}

我希望这对你有帮助,因为我工作得很好,问候

关于ios - 使用 UIAlertView 取消触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38091278/

相关文章:

Swift 3.0 c 风格 for 循环中的多个条件

swift - 如何在警报中设置键盘类型

ios - 如何在数组中传递这种格式字典中的数据

ios - 从 View 层次结构中删除 subview 并将其删除的正确方法是什么?

swift - 在 Vapor 中的/Views 中查找嵌套文件夹

swift - NSTableView 行第二次不可点击

ios - 在关闭动画之前运行 UIAlertController 完成处理程序 block

ios - 在 Swift 的 iOS 警报中从 TextField 获取输入值

ios - HTTP POST 请求,HTML 表单输入(登录 : user/password) browser simulation

ios - 如何在 EventKit 中使用 CalendarIdentifier 获取提醒?