2017-04-02 7 views
-1

XcodeのスウィフトプレイグラウンドでUILabelをクリックするまで、whileループを待ちます。これどうやってするの?ユーザーがUILabelをクリックするのを待つ方法は?

はここに私のループ例えば

func gameLoop() { 

    while(score >= 0) { 

     let n = arc4random_uniform(3) 

     if(n == 0) { 
      opt1.text = rightStatements.randomElement() 
      opt2.text = wrongStatements.randomElement() 
      opt3.text = wrongStatements.randomElement() 
     } else if(n == 1) { 
      opt1.text = wrongStatements.randomElement() 
      opt2.text = rightStatements.randomElement() 
      opt3.text = wrongStatements.randomElement() 
     } else if(n == 2) { 
      opt1.text = wrongStatements.randomElement() 
      opt2.text = wrongStatements.randomElement() 
      opt3.text = rightStatements.randomElement() 
     } 
    } 

} 

だ、私は、ユーザーが、opt2opt1クリックするか、opt3が次に何をユーザーがクリックに基づいて何かをするまで待機します。

+0

メインスレッドで何か起こるのを待って無限ループを実行することは決してありません。 'UILabel'を' UIButton'に変更し、 'IBAction'を使用してください。 –

+0

私はどのようにしてIBActionを作成しますか? –

答えて

1

ラベルの代わりにボタンを使用し、ボタンにタグ1、2、3を割り当てます。ボタンのIBAction機能を作成し、すべてのボタンを同じ機能に接続します。

変数 'n'をグローバルにします。

var n = Int() 

func nextAttempt() { 

    if(score >= 0) { 

     n = arc4random_uniform(3) 

     if(n == 0) { 
      opt1.text = rightStatements.randomElement() 
      opt2.text = wrongStatements.randomElement() 
      opt3.text = wrongStatements.randomElement() 
     } else if(n == 1) { 
      opt1.text = wrongStatements.randomElement() 
      opt2.text = rightStatements.randomElement() 
      opt3.text = wrongStatements.randomElement() 
     } else if(n == 2) { 
      opt1.text = wrongStatements.randomElement() 
      opt2.text = wrongStatements.randomElement() 
      opt3.text = rightStatements.randomElement() 
     } 
    } 
    else 
    { 
     //Score < 0 
     //Game Over 
    } 

} 


@IBAction func onButtonClick(_ sender: Any) 
{ 
switch(sender.tag) 
    { 
    case 1: 
     if (n==0) 
     { 
     //Right button tapped 
     //Update score if you want 
     } 
     else 
     { 
     self.nextAttempt() 
     } 
    case 2: 
    if (n==1) 
     { 
     //Right button tapped 
     //Update score if you want 
     } 
    else 
    { 
     self.nextAttempt() 
    } 
    case 3: 
    if (n==2) 
    { 
     //Right button tapped 
     //Update score if you want 
    } 
    else 
    { 
     self.nextAttempt() 
    } 
    } 
} 

関連する問題