按钮仅在两次敲击时起作用
我有两个按钮,每个按钮与另一个视图控制器都有不同的选择,只有在满足某些条件时才触发。由于某种原因,其中一个按钮可以正常工作,另一个按钮必须敲击两次,才能将其转移到下一个视图控制器上。奇怪的部分是,如果复制和粘贴条件并稍作修改以转到各自的VC,则两个按钮和SEGUE的代码。我如何在第一个点击中获得两个按钮?
到目前为止,我已经尝试过:
- 重新排序按钮函数
- 重新排序segue函数
- 删除/重新连接按钮
- 删除/重新连接按钮,
我相信它与以下代码有关该应用程序中的类似功能几乎具有相同的代码(减去下面的部分)效果很好。我确实需要它,但是由于测试了零/错误:
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return nextButton
}
这是代码
import UIKit
class bathShowerMeasurementViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}
var nextButton = false
@IBOutlet weak var inchesButton: UIButton!
@IBAction func inches(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
} else {
sender.isSelected = true
}
}
@IBOutlet weak var unitError: UILabel!
@IBOutlet weak var centimeterButton: UIButton!
@IBAction func centimeter(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
} else {
sender.isSelected = true
}
}
@IBOutlet weak var length: UITextField!
@IBOutlet weak var lenghtError: UILabel!
@IBOutlet weak var width: UITextField!
@IBOutlet weak var widthError: UILabel!
@IBOutlet weak var height: UITextField!
@IBOutlet weak var heightError: UILabel!
@IBAction func showerCalc(_ sender: UIButton) {
let widthTub = Double(width.text!)
let lengthTub = Double(length.text!)
let heightTub = Double(height.text!)
var enableNextButton = true
if widthTub == nil || widthTub == 0.0 {
widthError.text = "Enter Width"
enableNextButton = false
}
if heightTub == nil || heightTub == 0.0 {
heightError.text = "Enter Height"
enableNextButton = false
}
if lengthTub == nil || lengthTub == 0.0 {
lenghtError.text = "Enter Length"
enableNextButton = false
}
switch (inchesButton.isSelected, centimeterButton.isSelected) {
case (false, false):
unitError.text = "Please Select A Unit"
enableNextButton = false
case (true, true):
unitError.text = "Only Select One Unit"
enableNextButton = false
default: break
}
nextButton = enableNextButton
}
@IBAction func goToShower(_ sender: UIButton) {
let widthTub = Double(width.text!)
let lengthTub = Double(length.text!)
let heightTub = Double(height.text!)
var enableNextButton = true
if widthTub == nil || widthTub == 0.0 {
widthError.text = "Enter Width"
enableNextButton = false
}
if heightTub == nil || heightTub == 0.0 {
heightError.text = "Enter Height"
enableNextButton = false
}
if lengthTub == nil || lengthTub == 0.0 {
lenghtError.text = "Enter Length"
enableNextButton = false
}
switch (inchesButton.isSelected, centimeterButton.isSelected) {
case (false, false):
unitError.text = "Please Select A Unit"
enableNextButton = false
case (true, true):
unitError.text = "Only Select One Unit"
enableNextButton = false
default: break
}
nextButton = enableNextButton
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return nextButton
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let bathLength = Double(length.text!)
let bathWidth = Double(width.text!)
let bathHeight = Double(height.text!)
let water = Double(bathLength! * bathWidth! * bathHeight!)
if (segue.identifier == "showerCalc") {
if inchesButton.isSelected == true{
let bathWater = water / 231
let destinationVC = segue.destination as! showerFillMethodViewController
destinationVC.bathFlow = String(bathWater)
}
if centimeterButton.isSelected == true{
let bathWater = water / 3785.41
let destinationVC = segue.destination as! showerFillMethodViewController
destinationVC.bathFlow = String(bathWater)
}
}
if (segue.identifier == "showerTool") {
if inchesButton.isSelected == true{
let bathWater = water / 231
let destinationVC = segue.destination as! showerFlowMainViewController
destinationVC.bathFlow = String(bathWater)
}
if centimeterButton.isSelected == true{
let bathWater = water / 3785.41
let destinationVC = segue.destination as! showerFlowMainViewController
destinationVC.bathFlow = String(bathWater)
}
}
}
}
I have two buttons that each have a different segue to a different view controller, which is only triggered if certain conditions are met. For some reason, one of the buttons works fine, the other button has to be tapped twice in order for it to segue to the next view controller. The strange part is that the code for both buttons and segue if conditions are copied and pasted and slightly modified to go to their respective vcs. How can I get both buttons to work on the first tap?
So far I've tried:
- reordering the button functions
- reordering the segue functions
- deleting/reconnecting the button
- deleting/reconnecting the button
I believe it has something to do with the following code because when I remove it, this behavior is gone and a different but similar feature in the app with virtually the same code (minus the part below) works just fine. I do need it however due to testing for nil/errors:
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return nextButton
}
Here is the code
import UIKit
class bathShowerMeasurementViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}
var nextButton = false
@IBOutlet weak var inchesButton: UIButton!
@IBAction func inches(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
} else {
sender.isSelected = true
}
}
@IBOutlet weak var unitError: UILabel!
@IBOutlet weak var centimeterButton: UIButton!
@IBAction func centimeter(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
} else {
sender.isSelected = true
}
}
@IBOutlet weak var length: UITextField!
@IBOutlet weak var lenghtError: UILabel!
@IBOutlet weak var width: UITextField!
@IBOutlet weak var widthError: UILabel!
@IBOutlet weak var height: UITextField!
@IBOutlet weak var heightError: UILabel!
@IBAction func showerCalc(_ sender: UIButton) {
let widthTub = Double(width.text!)
let lengthTub = Double(length.text!)
let heightTub = Double(height.text!)
var enableNextButton = true
if widthTub == nil || widthTub == 0.0 {
widthError.text = "Enter Width"
enableNextButton = false
}
if heightTub == nil || heightTub == 0.0 {
heightError.text = "Enter Height"
enableNextButton = false
}
if lengthTub == nil || lengthTub == 0.0 {
lenghtError.text = "Enter Length"
enableNextButton = false
}
switch (inchesButton.isSelected, centimeterButton.isSelected) {
case (false, false):
unitError.text = "Please Select A Unit"
enableNextButton = false
case (true, true):
unitError.text = "Only Select One Unit"
enableNextButton = false
default: break
}
nextButton = enableNextButton
}
@IBAction func goToShower(_ sender: UIButton) {
let widthTub = Double(width.text!)
let lengthTub = Double(length.text!)
let heightTub = Double(height.text!)
var enableNextButton = true
if widthTub == nil || widthTub == 0.0 {
widthError.text = "Enter Width"
enableNextButton = false
}
if heightTub == nil || heightTub == 0.0 {
heightError.text = "Enter Height"
enableNextButton = false
}
if lengthTub == nil || lengthTub == 0.0 {
lenghtError.text = "Enter Length"
enableNextButton = false
}
switch (inchesButton.isSelected, centimeterButton.isSelected) {
case (false, false):
unitError.text = "Please Select A Unit"
enableNextButton = false
case (true, true):
unitError.text = "Only Select One Unit"
enableNextButton = false
default: break
}
nextButton = enableNextButton
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return nextButton
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let bathLength = Double(length.text!)
let bathWidth = Double(width.text!)
let bathHeight = Double(height.text!)
let water = Double(bathLength! * bathWidth! * bathHeight!)
if (segue.identifier == "showerCalc") {
if inchesButton.isSelected == true{
let bathWater = water / 231
let destinationVC = segue.destination as! showerFillMethodViewController
destinationVC.bathFlow = String(bathWater)
}
if centimeterButton.isSelected == true{
let bathWater = water / 3785.41
let destinationVC = segue.destination as! showerFillMethodViewController
destinationVC.bathFlow = String(bathWater)
}
}
if (segue.identifier == "showerTool") {
if inchesButton.isSelected == true{
let bathWater = water / 231
let destinationVC = segue.destination as! showerFlowMainViewController
destinationVC.bathFlow = String(bathWater)
}
if centimeterButton.isSelected == true{
let bathWater = water / 3785.41
let destinationVC = segue.destination as! showerFlowMainViewController
destinationVC.bathFlow = String(bathWater)
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论