2016-06-29 8 views
0

私はゲームを構築しています。UIAlertViewを使用してタッチをキャンセルする

このゲームの設定は&オプション機能のUITableViewControllerです。

選択できるオプションの1つは、相手をパスして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.) 
} 

私が午前問題は、入力のための警告コントローラが表示され、待機していることですが、それは継続didSelectRowAtIndexPath関数形の残りの部分を防ぐことはできません。

セルのタッチを処理する前に、ユーザー入力を受け取るにはどうすればよいですか?

+0

について。 と、ユーザーがキャンセルボタンを押した場合はどうなりますか? –

+0

@AbdulRehmanWarraich、ユーザーがキャンセルボタンを押した場合、私はdidSelectRowAtIndexPath関数を終了して、コードを追加しないで終了したいと思います。ユーザーが[続行]ボタンを押すと、通常通りにプログラムを続行します。 – zeeple

答えて

1

こんにちは、あなたのコードは、あなたの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) 
    } 

} 

でなければならない、あなたがユーザーの入力した後に実行したいんどのコード

1

悪い英語のために申し訳ありません。私があなたから質問するのは、ユーザーがUIAlertControllerのボタンの1つを選択するまでプログラムを実行し続けたいということです。

あなたが行う必要があることは、OKボタンまたはキャンセルボタンの動作の中でタスクを実行することです。

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

希望します。必要なのは、あなたのオプションを「確認」のためのあなたの完了部分の内側にあなたのコードを入れて

+0

残念ながら、これは私の問題の解決策ではありません。アクションボタン内の目的のアクションを処理しているにもかかわらず、didSelectRowAtIndexPath関数はUIAlertControllerからの入力を待たずに実行を続けます。 – zeeple

関連する問題