有没有办法根据之前是否在另一个视图控制器上按下按钮来编写 if 语句?
Is there a way to write an if statement based on if a button was pressed on another view controller previously?
我正在尝试创建一个计算器,向用户显示有关其大学成绩的结果。用户点击按钮并根据他们的 select 离子,UI 视图更新并显示下一个问题。我的代码一直有效,直到我需要它访问以前在另一个视图控制器上 selected 之前点击的按钮。我特别遇到的问题是在一个视图控制器上,用户可以 select 如果他们想知道 Calculation1(与带有 sender.tag == 1
的顶部按钮相关联)或 Calculation2(底部按钮,sender.tag == 2
).无论按下哪个按钮,都会在新的视图控制器上对相同的问题执行 segue。在回答了那个相互的问题之后,就只有一个按钮可以按下了。我正在尝试为如果 Calculation1(来自之前的 VC)被 selected 编写一个 if 语句,然后将视图更新到下一个问题,如果 Calculation2 被 selected 执行 segue为下一个问题换一个不同的 VC。我尝试使用布尔值的方式是作为之前 VC 的 属性。 if 语句不起作用,相反,无论 selection.
都会发生相同的操作
这是 iOS
应用 Swift 使用 Xcode。我试图创建一个布尔值作为 属性,以便在点击 Calculation1 时,findCalc1 = true
然后在下一个 VC 中作为 属性 访问该布尔值,但它没有工作。我还尝试为相互问题的 segue 准备一个布尔值,但它也没有用。我觉得我对不同 VC 之间可以执行和不能执行的操作的理解存在差距。
// 这是我第一个的代码 VC
@IBAction func lightButtonPressed(_ sender: UIButton) {
if sender.tag == 1 && lightViewPromptIndex == 1 {
desiredGpaText = textfield.text!
performSegue(withIdentifier: "goToDarkButtonVC", sender: self)
} else if sender.tag == 2 && lightViewPromptIndex == 1 {
performSegue(withIdentifier: "goToDarkButtonVC", sender: self)
}
// 这是我的第二个代码 VC
@IBAction func darkButtonPressed(_ sender: Any) {
if aboveTopPromptIndex == 1 && lightViewObject().findCalc1 == true {
aboveTopTextPrompt.text = aboveTopPrompt2
topTextfield.placeholder = "Ex: 76.00"
besideTopTextLabel.isHidden = true
underTopTextLabel.text = "course credits"
aboveBottomTextPrompt.text = "that count toward my GPA."
bottomTextfield.isHidden = true
underBottomTextLabel.isHidden = true
bottomFloatingLabel.isHidden = true
darkButton.setTitle(nextTitle, for: .normal)
aboveTopPromptIndex = 2
} else if aboveTopPromptIndex == 1 && lightViewObject().findCalc1 == false {
performSegue(withIdentifier: "darkViewToABC", sender: TextfieldTwoLightButtonsViewController.self)
相互提问有aboveTopPromptIndex == 1
。我想要发生的是,如果这个问题被问到并且 Calculation1 之前被 selected,对于 if 语句中的第一段代码 运行... 如果需要 Calculation2,对于segue 在 else if 语句中发生。相反,无论按下哪个按钮,if 语句的第一部分都会发生,而不管 Calculation2 之前是否被 selected。
创建一个新对象lightViewObject()会创建一个新的内存块,它不会包含之前存储的值viewController
使用 Prepare for a segue 将数据从一个视图控制器传递到另一个视图控制器
class lightViewController : UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "goToDarkButtonVC") {
let destVC = segue.destinationViewController as! darkViewController
destVC.findCalc1 = true // change the value here
}
}
@IBAction func lightButtonPressed(_ sender: UIButton) {
if sender.tag == 1 && lightViewPromptIndex == 1 {
desiredGpaText = textfield.text!
performSegue(withIdentifier: "goToDarkButtonVC", sender: self)
} else if sender.tag == 2 && lightViewPromptIndex == 1 {
performSegue(withIdentifier: "goToDarkButtonVC", sender: self)
}
}
}
然后使用darkViewController中的值
class darkViewController : UIViewController {
var findCalc1:Bool = false
@IBAction func darkButtonPressed(_ sender: Any) {
if aboveTopPromptIndex == 1 && self.findCalc1 == true {
//Your Code here
}
}
This problem is about passing data from one view controller to another view controller. To pass data from one view controller to
another view controller you can use the methods of segue
func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { }
用法:
将扩展添加到您的第一个视图控制器,即添加到您的 lightViewController
extension lightViewController {
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "goToDarkButtonVC") {
let destVC = segue.destinationViewController as! darkViewController
destVC. findCalc = sender as! Bool
}
}
@IBAction func lightButtonPressed(_ sender: UIButton) {
if sender.tag == 1 && lightViewPromptIndex == 1 {
desiredGpaText = textfield.text!
// In the sender set the value as per your need....
performSegue(withIdentifier: "goToDarkButtonVC", sender: true)
} else if sender.tag == 2 && lightViewPromptIndex == 1 {
// In the sender set the value as per your need....
performSegue(withIdentifier: "goToDarkButtonVC", sender: true)
}
}
}
在你的第二个视图控制器中访问值就像这样:
class darkViewController : UIViewController {
var findCalc = false
@IBAction func darkButtonPressed(_ sender: Any) {
if aboveTopPromptIndex == 1 && self.findCalc1 == true {
//TODO: add your handling code here....
}
}
这就是您可以轻松实现要求的方法。
我正在尝试创建一个计算器,向用户显示有关其大学成绩的结果。用户点击按钮并根据他们的 select 离子,UI 视图更新并显示下一个问题。我的代码一直有效,直到我需要它访问以前在另一个视图控制器上 selected 之前点击的按钮。我特别遇到的问题是在一个视图控制器上,用户可以 select 如果他们想知道 Calculation1(与带有 sender.tag == 1
的顶部按钮相关联)或 Calculation2(底部按钮,sender.tag == 2
).无论按下哪个按钮,都会在新的视图控制器上对相同的问题执行 segue。在回答了那个相互的问题之后,就只有一个按钮可以按下了。我正在尝试为如果 Calculation1(来自之前的 VC)被 selected 编写一个 if 语句,然后将视图更新到下一个问题,如果 Calculation2 被 selected 执行 segue为下一个问题换一个不同的 VC。我尝试使用布尔值的方式是作为之前 VC 的 属性。 if 语句不起作用,相反,无论 selection.
这是 iOS
应用 Swift 使用 Xcode。我试图创建一个布尔值作为 属性,以便在点击 Calculation1 时,findCalc1 = true
然后在下一个 VC 中作为 属性 访问该布尔值,但它没有工作。我还尝试为相互问题的 segue 准备一个布尔值,但它也没有用。我觉得我对不同 VC 之间可以执行和不能执行的操作的理解存在差距。
// 这是我第一个的代码 VC
@IBAction func lightButtonPressed(_ sender: UIButton) {
if sender.tag == 1 && lightViewPromptIndex == 1 {
desiredGpaText = textfield.text!
performSegue(withIdentifier: "goToDarkButtonVC", sender: self)
} else if sender.tag == 2 && lightViewPromptIndex == 1 {
performSegue(withIdentifier: "goToDarkButtonVC", sender: self)
}
// 这是我的第二个代码 VC
@IBAction func darkButtonPressed(_ sender: Any) {
if aboveTopPromptIndex == 1 && lightViewObject().findCalc1 == true {
aboveTopTextPrompt.text = aboveTopPrompt2
topTextfield.placeholder = "Ex: 76.00"
besideTopTextLabel.isHidden = true
underTopTextLabel.text = "course credits"
aboveBottomTextPrompt.text = "that count toward my GPA."
bottomTextfield.isHidden = true
underBottomTextLabel.isHidden = true
bottomFloatingLabel.isHidden = true
darkButton.setTitle(nextTitle, for: .normal)
aboveTopPromptIndex = 2
} else if aboveTopPromptIndex == 1 && lightViewObject().findCalc1 == false {
performSegue(withIdentifier: "darkViewToABC", sender: TextfieldTwoLightButtonsViewController.self)
相互提问有aboveTopPromptIndex == 1
。我想要发生的是,如果这个问题被问到并且 Calculation1 之前被 selected,对于 if 语句中的第一段代码 运行... 如果需要 Calculation2,对于segue 在 else if 语句中发生。相反,无论按下哪个按钮,if 语句的第一部分都会发生,而不管 Calculation2 之前是否被 selected。
创建一个新对象lightViewObject()会创建一个新的内存块,它不会包含之前存储的值viewController
使用 Prepare for a segue 将数据从一个视图控制器传递到另一个视图控制器
class lightViewController : UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "goToDarkButtonVC") {
let destVC = segue.destinationViewController as! darkViewController
destVC.findCalc1 = true // change the value here
}
}
@IBAction func lightButtonPressed(_ sender: UIButton) {
if sender.tag == 1 && lightViewPromptIndex == 1 {
desiredGpaText = textfield.text!
performSegue(withIdentifier: "goToDarkButtonVC", sender: self)
} else if sender.tag == 2 && lightViewPromptIndex == 1 {
performSegue(withIdentifier: "goToDarkButtonVC", sender: self)
}
}
}
然后使用darkViewController中的值
class darkViewController : UIViewController {
var findCalc1:Bool = false
@IBAction func darkButtonPressed(_ sender: Any) {
if aboveTopPromptIndex == 1 && self.findCalc1 == true {
//Your Code here
}
}
This problem is about passing data from one view controller to another view controller. To pass data from one view controller to another view controller you can use the methods of segue
func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { }
用法:
将扩展添加到您的第一个视图控制器,即添加到您的 lightViewController
extension lightViewController {
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "goToDarkButtonVC") {
let destVC = segue.destinationViewController as! darkViewController
destVC. findCalc = sender as! Bool
}
}
@IBAction func lightButtonPressed(_ sender: UIButton) {
if sender.tag == 1 && lightViewPromptIndex == 1 {
desiredGpaText = textfield.text!
// In the sender set the value as per your need....
performSegue(withIdentifier: "goToDarkButtonVC", sender: true)
} else if sender.tag == 2 && lightViewPromptIndex == 1 {
// In the sender set the value as per your need....
performSegue(withIdentifier: "goToDarkButtonVC", sender: true)
}
}
}
在你的第二个视图控制器中访问值就像这样:
class darkViewController : UIViewController {
var findCalc = false
@IBAction func darkButtonPressed(_ sender: Any) {
if aboveTopPromptIndex == 1 && self.findCalc1 == true {
//TODO: add your handling code here....
}
}
这就是您可以轻松实现要求的方法。