我有一个标签,当FirstViewController的视图第一次打开时,它会出现在NavigationBar中。标签显示了一个应该可以更改的数字。如果我单击FirstViewController上的一个按钮,SecondViewController的视图就会出现,在其中的NavigationBar中,FirstViewController的标签仍然可见。当我通过单击SecondViewController中的按钮来更改标签中写入的编号时,只有当我返回到FirstViewController的视图时,标签的编号才会更改。这是因为我在ViewDidLoad字符串循环中更新了标题。现在我的问题是:我希望在单击SecondViewController中的按钮时更改标签的编号,尽管标签是在FirstViewController的代码中定义的。标签的数字显示了钱的数量。
这是FirstViewController的代码:
var moneyLabel: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationLabel()
let newMoney:String = String(format: "%f", money)
updateTitle(title: "\(newMoney)")
}
func updateTitle(title: String) {
if let myTitleView = self.moneyLabel {
myTitleView.text = title
}
}
func setupNavigationLabel() {
let navigationBar = self.navigationController?.navigationBar
let moneyFrame = CGRect(x: 300, y: 0, width:
(navigationBar?.frame.width)!/2, height: (navigationBar?.frame.height)!)
moneyLabel = UILabel(frame: moneyFrame)
moneyLabel?.text = "\(money)"
navigationBar?.addSubview(moneyLabel!)
}
发布于 2018-06-07 04:17:03
您的问题似乎出在第二个ViewController
上,但是您没有共享任何代码!使用下面的方法来实现你的目标。
class secondViewControler:UIViewController{
var money:Int
var moneyLabel:UILAbel?
override func viewDidLoad(){
self.money= //pass the amount, i dont know which way you use to store and retrieve the amount
moneyLabel.text="\(money)"
}
@IBAction updateMoney(){
//get the amount from the textfield say the output is X
money=X
moneyLabel.text="\(money)"
}
}
https://stackoverflow.com/questions/50727895
复制相似问题