2016-06-29 5 views
0

나는 게임을 만들고 있습니다.UIAlertView를 사용하여 터치 취소

이 게임의 설정은 & 옵션 기능으로 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.) 
} 

오전 데 문제가 입력에 대한 경고 컨트롤러 디스플레이와 대기,하지만 계속 didSelectRowAtIndexPath 기능 양식의 나머지 부분을 방해하지 않는다는 것입니다 다음과 같이

내 코드입니다.

셀 터치를 처리하기 전에 사용자 입력을 어떻게 받습니까?

+0

간주해야합니다. 사용자가 취소 버튼을 누르면 어떻게됩니까 ?? –

+0

@AbdulRehmanWarraich, 사용자가 취소 버튼을 누르면 추가 코드를 실행하지 않고 didSelectRowAtIndexPath 함수를 종료하고 싶습니다. 사용자가 계속 버튼을 누르면 프로그램을 평소와 같이 계속 진행합니다. – zeeple

답변

1

안녕하세요 당신이 당신의 "확인"옵션에 대한 귀하의 완료 섹션의 내부 코드를 넣어 필요로하는 것은, 당신의 코드는 실행됩니다 코드는 내가 나에게 좋은 작품이, 당신이 도움이되기를 바랍니다

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에서 버튼 중 하나를 선택할 때까지 계속 실행하도록 프로그램을 보류하고 싶다는 것입니다.

당신이해야 할 일은 확인 버튼이나 취소 버튼의 동작 내부에서 작업을 수행하는 것입니다.

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

희망 하시겠습니까? 에 사용자 탭 그래서 당신의 당신의 UIAlertViewController

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

의 '확인'버튼을 때

+0

불행히도 이것은 내 문제의 해결책이 아닙니다. 액션 버튼 내에서 원하는 액션을 처리 함에도 불구하고 didSelectRowAtIndexPath 함수는 UIAlertController로부터의 입력을 기다리지 않고 계속 실행됩니다. – zeeple

관련 문제