不同ViewController上的Xcode Swift导航栏
当FirstViewController的视图第一次打开时,导航栏中会出现一个标签。标签显示了一个应该可以更改的数字。如果单击FirstViewController上的按钮,则第二个ViewController的视图将显示在其导航栏中,FirstViewController的标签仍然可见。当我通过单击SecondViewController中的按钮来更改写入标签中的编号时,标签的编号仅在我返回到FirstViewController的视图时更改。这是因为我更新了ViewDidLoad字符串循环中的标题。 现在我的问题是: 我希望在单击SecondViewController中的按钮时更改标签的编号,尽管标签是在FirstViewController的代码中定义的。标签上的数字表示钱的数量 这是FirstViewController的代码:不同ViewController上的Xcode Swift导航栏,swift,xcode,label,navigationbar,Swift,Xcode,Label,Navigationbar,当FirstViewController的视图第一次打开时,导航栏中会出现一个标签。标签显示了一个应该可以更改的数字。如果单击FirstViewController上的按钮,则第二个ViewController的视图将显示在其导航栏中,FirstViewController的标签仍然可见。当我通过单击SecondViewController中的按钮来更改写入标签中的编号时,标签的编号仅在我返回到FirstViewController的视图时更改。这是因为我更新了ViewDidLoad字符串循环中
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!)
}
看来您的问题出在第二个
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)"
}
}
分享你的代码。。。使用本指南编辑您的问题并在此处添加任何代码,您使用的是两个VC提供的代码,您是否将数据从一个VC传递到另一个VC?您正在从服务器下载它们吗?您必须提供足够的信息,以便共享代码!现在添加代码