我正在尝试创建一个向用户显示有关其大学成绩的计算器。用户点击按钮,然后基于其选择,UI视图将更新并显示下一个问题。直到我需要它访问以前在其他视图控制器上选择的先前点击的按钮时,我的代码才能工作。我特别遇到的问题是在一个视图控制器上,用户可以选择是否要知道Calculation1(与带有sender.tag == 1
的顶部按钮相关联)或Calculation2(底部按钮,sender.tag == 2
)。无论按下哪个按钮,都会在新的视图控制器上对同一问题执行搜索。共同的问题回答后,只需按一个按钮。我正在尝试为if语句编写一个if语句,用于是否选择了Calculation1(来自以前的VC),然后将视图更新到下一个问题,如果选择了Calculation2,则对下一个问题执行到另一个VC的选择。我尝试使用布尔值的方式是作为先前VC的属性。 if语句不起作用,无论选择什么,都会执行相同的操作。
这是针对使用Xcode的Swift的iOS
应用程序。我试图创建一个布尔值作为属性,以便在点击Calculation1时findCalc1 = true
,然后在下一个VC中将该布尔值作为属性访问,但是它没有用。我也尝试过为布尔值准备一个布尔值,以解决相互提问的问题,但这也不起作用。我感觉我对不同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,以便运行if语句中的第一段代码...,如果需要Calculation2,则要在else if语句中进行选择。相反,无论按下哪个按钮,if语句的第一部分都会发生,而不管先前是否选择了Calculation2。
答案 0 :(得分:0)
创建一个新对象lightViewObject()将创建一个新的内存块,并且不包含存储在先前viewController中的值
使用Prepare for 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
}
}
答案 1 :(得分:0)
此问题与将数据从一个视图控制器传递到另一视图控制器有关。将数据从一个视图控制器传递到 您可以使用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....
}
}
这就是您可以轻松满足要求的方式。