简体   繁体   中英

Using UIAlertView to Cancel a Touch

I am building a game.

This game has a UITableViewController which serves as a Settings & Options feature.

One of the options that you can select is to switch the opponent from pass and play to an AI (or the other way around).

What I'd like to do is use a UIAlertController to intercept the touch, check for a game in progress, and prompt the user to cancel the change or continue.

I have implemented the code to do this within the override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) function.

My code is as follows:

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.)
}

The problem I am having is that the alert controller displays and waits for input, but it does not prevent the rest of the didSelectRowAtIndexPath function form continuing.

How do I receive the users input before handling the touch on the cell?

Sorry for the bad English. What I understand from you question is that you want to hold the program to continue execution till user select one of the button from UIAlertController.

for that you need to do is perform task inside the action of Ok button or Cancel button.

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

                     //Do something in here like print 
                      println("Confirm Button Pressed")



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

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

hope it help.

Hello All you need is put your code inside of your completion section for your "Confirm" option, your code will be execute only when user tap on "Confirm" button of your UIAlertViewController

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

so your code must be

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)
    }

}

I hope this helps you, for me works great, regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM