2016-07-31 14 views
0

私のUITextFieldsの入力が1050未満のときにアラートが表示されるようなコードを記述しました。入力がそれを満たすときに表示されますが、即座に再表示されます。UIAlertControllerは終了後も再表示されます

以下

viewDidLoad関数のコードです:

override func viewDidLoad(){ 
    super.viewDidLoad() 
    alert = UIAlertController(title: "Error", message: "Please enter an exit width value greater than 1050", preferredStyle: UIAlertControllerStyle.Alert) 
    let okay = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: valueCalc) 
    alert.addAction(okay) 
} 

は、それから私は(ボタンをタップしたときに呼び出される)私のvalueCalc機能を持っている:

@IBAction func valueCalc(sender: AnyObject){ 
    if(Int(mmText.text!)! < 1050){ //mmText is an UITextField 
     self.presentViewController(alert, animated: true, completion: nil) 
    } 
} 

答えて

4

を試してみてください。

また、指定された文字が少なくなると警告が表示される値が計算されます。その代わりの

、あなたのコードでは、この行を置き換える -

let okay = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: handlerMethod) 

をし、あなたのコードに、このメソッドを追加

func handlerMethod() { 

    //handle your action here after ok is pressed for e.g if you wanna just dismiss the alert then write 

    dismissViewControllerAnimated(true, completion: nil) 

} 
1

あなたはhandler引数を持っていますあなたのUIAlertActionvalueCalcに設定されています。したがって、ユーザーが「OK」をタップするたびに、方法valueCalcが再び実行され、値は(おそらく)同じであるため、アラートが再び表示されます。あなたはOKを押したときに

0

が呼び出されるコード

let okay = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: valueCalc) 

あなたのハンドラ名valueCalcのあなたのラインによると、この

override func viewDidLoad(){ 
    super.viewDidLoad() 
    alert = UIAlertController(title: "Error", message: "Please enter an exit width value greater than 1050", preferredStyle:  UIAlertControllerStyle.Alert) 

    let okay = UIAlertAction(
     title: "OK", 
     style: UIAlertActionStyle.Destructive) { (action) in } 

    } 

    @IBAction func valueCalc(sender: AnyObject){ 
    if(Int(mmText.text!)! < 1050){ //mmText is an UITextField 
    self.presentViewController(alert, animated: true, completion: nil) 
    } 
関連する問題