【问题标题】:access IBOutlet button from other view controllers从其他视图控制器访问 IBOutlet 按钮
【发布时间】:2015-12-09 15:08:40
【问题描述】:

我需要访问和更改另一个视图控制器上存在的按钮的标题。这可能吗?如果是这样,我怎样才能做到这一点?

视图控制器 1:

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

视图控制器 2:

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

我想我在目标 C 中找到了解决方案,但我不知道如何快速完成此操作 Access IBOutlet from another class

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 2017-12-27
      相关资源
      最近更新 更多