I want to know how to access an @IBOutlet declared in a class from another class
for example, I have a class named myfirstview
class MyFirstView: UIViewController {
@IBOutlet var lblred: UILabel! = UILabel()
}
I want to change the text of the lblred
from another class named MySecondView
which is written in another .swift
file:
class MySecondView: UIViewController {
func modify() {
let mfv = MyFirstView()
mfv.lblred.text = "Hello"
}
}
But nothing happens.
I have connected lblred
with a storyboard label. I have searched a lot about this on the web but I can't find the one which can solve my problem. Please help me solve this problem.
Thank you.
@Sheen is correct about your immediate problem, but your problem is deeper. No object should access another object's IBOutlets. It's not well defined what will happen. This has been a long source of bugs in ObjC code, and Swift escalates those common bugs to crashes.
IBOutlets are not assigned until the view is loaded. This means that if the view controller is allocated but has not been put on the screen yet, the IBOutlets are still nil. Accessing an implicitly unwrapped nil will crash Swift.
View controllers should only communicate with their children view controllers. They should not communicate with arbitrary view controllers in the system. Communication between arbitrary view controllers is done via the model. One view controller updates the model, and another view controller reads from the model. This is the Model-View-Controller pattern that most of Cocoa is built around.
View controllers may interact more directly with their children, but still should not modify IBOutlets directly. They should set properties. It is the child view controller's responsibility to move that data from the property to the label at the correct time (which may have to wait until viewDidLoad()
). That's why it's called the "view controller." It is the one object responsible for its views. No one else should mess with them.