2016-07-31 2 views
0

내 UITextField 중 하나의 입력이 1050 미만일 때 경고가 표시되도록 코드를 작성했습니다. 입력이 만족되면 성공적으로 나타납니다. 그러나 "확인"을 누른 후 성공적으로 나타납니다 즉시 다시 나타납니다. 당신은 handler 인수를UIAlertController가 닫힌 후 다시 나타납니다.

@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

:

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 기능에있다 : 아래

viewDidLoad 기능의 코드입니다 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) 
    }