【问题标题】:Action button works only once操作按钮只能使用一次
【发布时间】:2017-12-21 23:07:00
【问题描述】:

我正在开发一个 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

    }
}

【问题讨论】:

  • Calcul 在做什么?它是否执行任何 UI 任务?
  • Calcul 进行了大量的物理计算并将结果放入图表和 4 个标签中@Mukesh
  • 您是否使用断点来证明当您触摸按钮时该方法只运行一次(非常不可能)?当您使用调试器单步执行代码时,您的逻辑是否正确?
  • @RoboticCat 是的,我什么都试过了,按钮上的动作只在第一次起作用

标签: ios swift xcode button action


【解决方案1】:

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

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 的代码而言,我将简单地按照描述移动声明/定义,并将实际的计算逻辑移动到警报操作对象中的处理程序调用的其自己的函数中。当然,条件检查仍然可以存在于操作方法中。不过,我敢肯定他现在已经找到了自己的解决方案。

【讨论】:

    【解决方案2】:

    该功能不会运行一次,而且每次都有效。但是你总是调用相同的参数

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

    在函数外声明这个参数

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-24
        • 2014-02-22
        • 2023-03-26
        • 1970-01-01
        相关资源
        最近更新 更多