您可以从照片中看到我的UI设置。 目标是当按下按钮时,如果数字大于9,第一个视图控制器上的标签将更改为“数字大于9”,如果小于9,则应触发segue并且第二个视图上的标签应更改到“数字大于9 .. 这是我到目前为止所做的,但是当触发segue时,secondView上的标签不会改变..
class ViewController: UIViewController {
@IBOutlet weak var labelFirst: UILabel!
@IBOutlet weak var numberEntered: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
var number: Int? {
get{
return Int(numberEntered.text!)
}
}
if number>9 {
labelFirst.text="number is greater than 9"
return false}
else {
return true
func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier=="segue"){
let destinationVC:secondView = segue.destinationViewController as! secondView
destinationVC.outputText="number is smaller than 9"
}
}
}
}
}
segue连接到按钮并指向第二个视图。
secondView:
import UIKit
class secondView: UIViewController {
@IBOutlet weak var label: UILabel!
var outputText=String()
override func viewDidLoad() {
label.text=outputText
}
}
答案 0 :(得分:0)
您的prepareForSegue方法位于错误的位置,它应该在shouldPerfomSegue方法之外,并标记为覆盖。
答案 1 :(得分:0)
它在第二个屏幕上没有更新的原因是因为你在viewDidLoad中进行更新。这只发生过一次。将代码移动到viewWillAppear。
import UIKit
class secondView: UIViewController {
@IBOutlet weak var label: UILabel!
var outputText=String()
override func viewWillAppear() {
super.viewWillAppear()
label.text = outputText
}
}