I'm trying to change the properties of a few IBOutlets from a different swift file and class. Ex: run function in class B (type UIView) to alter the alpha of IBOutlet in class A (type UIViewController). I've tried many different ways of accessing them (extensions,superclasses) but I still get nil values for these outlets. I am aware that this is because the view is allocated for but not loaded, but I cannot find a way to store and access class "A"'s outlets correctly to avoid this. Here is my code from class "B" :
let mainView = MainViewController() //Code in B
var changeAlpha = mainView.changeText() //Code in B
func changeAlpha() { //Code in class MainViewController
self.hotLabel.alpha = 0
}
Thanks in advance!
Don't try to allocate the MainViewController
again.
Try to access the existing MainViewController
instead.
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.nextResponder()
if let mainView = parentResponder as? MainViewController {
var changeAlpha = mainView.changeText()
}
}
When you try to create an object of MainViewController
class (which is already includes your data), the values that you have stored earlier will not be accessible because the reference will be different for these two class objects
You have to pass the existing MainViewController
class into the B class or try to access the existing MainViewController
or implement the messaging concepts.
The only way to solve these kind of issues is, try to study the OOPS basic concepts (especially object life cycle, allocation etc..) before writing the code.