UIAlertController在关闭后保持重新出现

时间:2016-07-31 16:09:21

标签: ios iphone swift alert uialertcontroller

我编写了一个警报代码,当我的一个UITextFields中的输入小于1050时,它会出现。当输入满足时,它会成功显示,但是在我按下" OK"它会立即重新出现。

以下是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)
    }
}

3 个答案:

答案 0 :(得分:5)

根据您的代码行

    def run(self):
        last = -infty
        self.winRunning = True
        ...
        while self.winRunning:
            # 4.a. Calc geometry
            self.calcFunc( self.get_sliders_as_list() )
            # 4.b. Send new coords to segments
            self.simFrame.transform_contents()
            # 4.d. Wait remainder of 40ms
            elapsed = time.time() * 1000 - last
            if elapsed < 40:
                time.sleep( (40 - elapsed) / 1000.0 )
            # 4.e. Mark beginning of next loop
            last = time.time() * 1000
            # 4.f. Update window
            if not self.winRunning: # This does not solve the problem 
                return # still tries to call 'update', 
                #        and never exits cleanly
            self.canvas.update() 
            # don't know how to prevent these from being called 
            # again after the window is destroyed
            self.rootWin.update_idletasks()

确定时,将调用您的处理程序名称​​ valueCalc

再次计算一个值,当出现的值小于指定的字符时,会显示警报。

而不是在代码中替换此行 -

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

并将此方法添加到您的代码

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

答案 1 :(得分:1)

您的handler设置为UIAlertAction的{​​{1}}参数。因此,每当用户点击&#34; OK&#34;时,方法valueCalc再次运行,并且由于该值(大概)仍然相同,警报会再次出现。

答案 2 :(得分:0)

试试这个

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