gpt4 book ai didi

ios - 从其他 View Controller 访问 IBOutlet 按钮

转载 作者:行者123 更新时间:2023-11-28 06:54:01 25 4
gpt4 key购买 nike

我需要访问和更改存在于另一个 View Controller 上的按钮的标题。这可能吗?如果是这样,我该如何实现?

View Controller 1:

Class Home: UIViewController
{
@IBOutlet weak var testBTN: UIButton!
}

View Controller 2:

Home.testBTN // does not work I can't access the button from here

我想我在 objective-c 中找到了解决方案,但我不知道如何快速完成 Access IBOutlet from another class

最佳答案

VC 2(发送通知):

let updateNotificationString = "com.yourcompany.yourapp.updatebutton"
NSNotificationCenter.defaultCenter().postNotificationName(updateNotificationString, object: self)

VC 1(接收和更新):

在viewDidLoad中:

let updateNotificationString = "com.yourcompany.yourapp.updatebutton"
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateButton", name: updateNotificationString, object: nil)

然后创建一个函数:

func updateButton() {
testBTN.setTitle("New Title", forState: UIControlState.Normal)
}

关于ios - 从其他 View Controller 访问 IBOutlet 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34182182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com