Computer Im
Computer Im

Reputation: 85

Swift: set @IBOutlet static

I want change @IBOutlet text value from another class

How i can set @IBOutlet to static ?

Below code not work:

@IBOutlet internal static var boxGender: UIView!

Upvotes: 1

Views: 991

Answers (2)

ymutlu
ymutlu

Reputation: 6715

You can set a static value of your controller and get all values from them. But this is not a right way to reach your outlets. You should pass your controller to other class instance.

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

    override func viewDidLoad() {
        super.viewDidLoad()
        Test.instance = self
    }
}
Test.instance?.boxGender

Upvotes: 3

Durdu
Durdu

Reputation: 4849

An IBOutlet cannot be static. (The compiler will give the error 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"
        }   
 }

Upvotes: 1

Related Questions