Swift:将@IBOutlet设置为静态

时间:2018-07-23 05:45:20

标签: swift static iboutlet

我想从另一个班级更改@IBOutlet的文本值

我如何将@IBOutlet设置为静态?

以下代码不起作用:

@IBOutlet internal static var boxGender: UIView!

2 个答案:

答案 0 :(得分:0)

IBOutlet不能为静态。 (编译器将给出错误Only instance properties can be declared @IBOutlet

 class SomeViewWithALabel: UIView {        
        @IBOutlet weak var myLabel: UILabel!    
        // ... methods and properties                
 }


 class MyController: UIViewController {        
        @IBOutlet weak var someViewWithALabel: SomeViewWithALabel!    

        //...

        override func viewWillAppear(_ animated: Bool) {
             super.viewWillAppear(animated)
             someViewWithALabel?.myLabel.text == "Custom text"
        }   
 }

答案 1 :(得分:0)

您可以设置控制器的静态值并从中获取所有值。但这不是到达网点的正确方法。您应该将控制器传递给其他类实例。

class Test: UIViewController {
    var boxGender: UIView!
    static var instance: Test?

    override func viewDidLoad() {
        super.viewDidLoad()
        Test.instance = self
    }
}
Test.instance?.boxGender
相关问题