0

I have two view controllers, FirstViewController and FourthViewController. FirstViewController is my initial view controller. I have a UILabel, mainLab, in FirstViewController. I present FourthViewController with

[self presentViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"FourthViewController"] animated:YES completion:nil];

Then, I use delegates to try changing the text of mainLab in FirstViewController, after FourthViewController is pushed. This, however, isn't changing the text of mainLab. I checked and mainLab does become null after the view controller is pushed. That being said, how would I change the label's text while FourthViewController is pushed even though mainLab gets nullified by FourthViewController's pushing?

3
  • Put some code of delegate method.
    – CRDave
    Aug 13, 2013 at 7:11
  • I don't believe the delegate method is the problem because I know the method is being called. The problem is that mainLab gets nullified when FourthViewController is presented, and I need to change the text of mainLab while FourthViewController is pushed. Aug 13, 2013 at 7:18
  • The delegate mehtod can still be the problem even if you know it is called. The key questions are: When is it called, before or after viewDidLoad and of which instance of the view controller Object is it called? Aug 13, 2013 at 7:57

1 Answer 1

0

You should show some of your code. However, this is my guess that I take from similar experiences that I had before I understood the view controller's life cycle (which is quite well documented BTW).

I assume that you set the UIControls (UILabel etc) directly in your delegate methods. If that is true: Don't do hat. Do not access any IBOutlet or view item directly from without a view controller.

Instead declare a property that is an NSNumber or NSString, just well suited to carry the data that you want to set. Provide a custom setter that sets the related UILabel etc. too but only, if the it is not nil. (You may as well declare private instance variables and just provde the quasi setter method.) In your viewDidLoad method, too, set the UILables etc. according to the values of the properties.

What happens in your case is that you set, lets say the text of a UILable before or even just after presentViewController. In your single line of code you even instanciate the view controller and then present it. Take that literally. The view controller is instanciated. Meaning a new one is created and all values get their defaults (which might be nil in some cases or empty strings or whatever has been set in the storyboard).

But even if you acces the UILabel (etc.) from the calling view controller directly after that, there is no guarantee that the newly instanciated view controller has been fully loaded in the meantime. In the event that the new view controller is loaded a bit later then your settings will be overwritten again. Unless you follow my advice.

There are other "hacks" that aim to force loading of the new view controller before actually setting its view items values. I tried one or two of them and they worked fine. But there is not guarantee that they will work in the future too and that is why I suggest to do it properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.