首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >不同ViewControllers上的Xcode Swift导航栏

不同ViewControllers上的Xcode Swift导航栏
EN

Stack Overflow用户
提问于 2018-06-07 03:09:15
回答 1查看 30关注 0票数 0

我有一个标签,当FirstViewController的视图第一次打开时,它会出现在NavigationBar中。标签显示了一个应该可以更改的数字。如果我单击FirstViewController上的一个按钮,SecondViewController的视图就会出现,在其中的NavigationBar中,FirstViewController的标签仍然可见。当我通过单击SecondViewController中的按钮来更改标签中写入的编号时,只有当我返回到FirstViewController的视图时,标签的编号才会更改。这是因为我在ViewDidLoad字符串循环中更新了标题。现在我的问题是:我希望在单击SecondViewController中的按钮时更改标签的编号,尽管标签是在FirstViewController的代码中定义的。标签的数字显示了钱的数量。

这是FirstViewController的代码:

代码语言:javascript
复制
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!)
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-07 04:17:03

您的问题似乎出在第二个ViewController上,但是您没有共享任何代码!使用下面的方法来实现你的目标。

代码语言:javascript
复制
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)"
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50727895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档