“操作”按钮仅适用一次

时间:2017-07-17 12:01:31

标签: ios swift xcode button action

我正在开发一个ios应用程序,我有一个按钮,每次点击都需要运行大量计算,但按钮操作仅在第一次运行。任何人都可以帮我这个吗?

这是按钮的代码:

@IBAction func Calculate_btn(_ sender: UIButton) {

    s2 = 0.0
    mintext.text = ""
    maxtext.text = ""
    avtext.text = ""

    if speedUser.text == "" {
        let alert = UIAlertController(title: "Error", message: "Give the speed to calculate the new temperature !", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }

    else if (Int(speedUser.text!)!<0) || (Int(speedUser.text!)!>20) {
        let alert = UIAlertController(title: "Error", message: "Give a valid speed : between 0 and 20 Km/h!", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)

    }

    else {
        refTot = ref + ref2
        rcfTot = rcf + rcf2
        airPermTot = airPermFab + airPermFab2

        print("refTot",refTot)
        print("rcfTot",rcfTot)
        print("airPermTot",airPermTot)

        var s2 = (speedUser.text! as NSString).floatValue

        Calcul(s: s2, rc: refTot, re: rcfTot, air: airPermTot)

        s2 = 0

    }
}

3 个答案:

答案 0 :(得分:0)

如果您尝试在按钮内重新加载数据,则此链接类似。他们在按钮内调用一个函数:) reloadData() isn't work in swift

答案 1 :(得分:0)

该函数不会一次运行,而是每次都能运行。但是您总是调用相同的参数

s2 = 0.0
mintext.text = ""
maxtext.text = ""
avtext.text = ""

从功能中声明此参数

答案 2 :(得分:0)

对于仍在寻找此问题答案的任何人,请尝试在viewDidLoad()之前使用空的初始化程序声明您的UIAlertController(以及伴随的动作)。然后,在viewDidLoad()中,使用一些参数以及您想要伴随的任何UIAlertActions构造它。由于某种原因(我不太清楚为什么),当您将警报控制器声明为不可变的并将其动作全部添加到同一IBAction / class函数中时,它似乎在第一次之后就无法实现其目标。我有一个与此问题完全相同的应用程序。警报将每次出现,但是处理程序功能将无法完成其任务。也没有抛出任何错误。例如,这是我想出的有效结构:

class SomeClass: UIViewController, OtherSubClasses
{
     var alertDialog: UIAlertController = UIAlertController()
     var ok: UIAlertAction = UIAlertAction()
     var cancel: UIAlertAction = UIAlertAction()
     // anything else needed

     override func viewDidLoad()
     {
          super.viewDidLoad()

          // define the alert dialog controller
          alertDialog = UIAlertController(title: "some title", 
               message: "some message to user", preferredStyle: .alert)

          // defines OK button for alert dialog
          ok = UIAlertAction(title: "OK", style: .default, handler:
          {
              (action) -> Void in
                // compartmentalized handler in the form of a function,
                // called with the self prefix if defined within the same file
                  self.someHandlerFunc()
          })

          // defines cancel button for alert dialog
          cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

          // now add them to the controller
          alertDialog.addAction(ok)
          alertDialog.addAction(cancel)
     }

     func someHandlerFunc()
     {
          // handler function logic called in whichever action needed
     }

     @IBAction func someButtonPressed(_ sender: Any)
     {
          // here is where you want to place logic for when the button is pressed,
          // then simply present the alert dialog that is standing by

          self.present(alertDialog, animated: true, completion, nil)
     }
}

通过这种方式,它不仅消除了一次性使用问题,而且还可以重用相同的警报控制器,并根据需要在其他任何操作功能中对其进行更改。例如,更改消息很简单:

alertDialog.message = "some new message to the user"

就OP的代码而言,我只需按所述移动声明/定义,然后将实际的计算逻辑移动到警报动作对象中的处理程序调用的其自己的函数中。当然,条件检查仍可以存在于action方法中。但是,我确定他目前已经遇到了自己的解决方案。