When does initWithNibName initialize the view controller's objects?
Weird issue I'm having...I have a UIViewController class that contains another UIViewController within it (to present it as a modal view). Within the viewDidLoad of first view controller, I call initWithNibName to instantiate the contained view controller.
That's fine, except that I have a UITextView in the contained view controller, and it's still a null object even after the initWIthNibName on the first view controller. Is this expected? (Lazy-loading???) When does that UITextView actually get created, when the view is ready to appear?
The view isn't set up at that point. Once it is you will get a call to viewDidLoad. When is it ready to appear? > viewWillAppear. When did it appear? > viewDidAppear. You have all those methods in the view controller.
The view isn't set up at that point. Once it is you will get a call to viewDidLoad. When is it ready to appear? > viewWillAppear. When did it appear? > viewDidAppear. You have all those methods in the view controller.
Thanks...I understand all the usage of those other methods. My issue is that I want to configure an object in that second view (just a UITextView text box) before the view is loaded. Or at least before it's visible.
Hmmm...how can you "load" a modal view but not make it visible?
My alternative so far involves making copies of objects...not good.
What specifically do you want to configure? You can create properties for all the things you want to configure. Then in the viewdidLoad of that viewcontroller, you just do read the values of the properties to actually set up the view to be rendered.
What specifically do you want to configure? You can create properties for all the things you want to configure. Then in the viewdidLoad of that viewcontroller, you just do read the values of the properties to actually set up the view to be rendered.
I want to edit an object (a UITextView), which is also a property, and also connected to a nib element, and that resides in a view controller class...for a modal view which the user may/may not invoke. But I want to get/edit the property before the view is visible. So my guess was after calling initWithNibName:MyNib, which I would have expected to also alloc/init the UITextView property (because it's connected into MyNib). That doesn't work, so I'm looking for another point at which the property will be initialized. It is when the corresponding view appears, but I don't want to have to go down the path of making the view visible. So I don't see how I can use viewWillAppear.
My "work around" is to alloc/init the UITextView myself, which is very rough. And if the user does finally invoke the modal view, that UITextView property is re-alloced/re-inited, values are lost, memory mgmt gets messy, etc, etc.
You should NEVER EVER EVER EVER EVER EVER directly edit any UI elements of a view controller from an external class. If you want to edit a specific property of the text view, then create a property in your view controller class for that specific textview property. Then in viewdidload of your viewcontroller, set the property of the textview to the property you created in the view controller.
The view isn't set up at that point. Once it is you will get a call to viewDidLoad. When is it ready to appear? > viewWillAppear. When did it appear? > viewDidAppear. You have all those methods in the view controller.
Interesting...but when does viewDidLoad get called? Not after initWithNibName, but after something more view-ish like addSubview or presentModalViewController, right?
I don't want to load my view. But it's looking like I may have to...maybe I'll add it to the bottom of the view hierarchy. Then it's there if the user does want to see that view.
You should NEVER EVER EVER EVER EVER EVER directly edit any UI elements of a view controller from an external class. If you want to edit a specific property of the text view, then create a property in your view controller class for that specific textview property. Then in viewdidload of your viewcontroller, set the property of the textview to the property you created in the view controller.
I have UITextView defined as a property, but I guess you're advising make a separate property entirely (unaffiliated with the nib). That's essentially what I ended up doing as a workaround. It just involves maintaining copies of data before and after view loading. Sounds like it may be the best I can do. Thanks.
Correct. For example, if you wanted to set the text in your UITextView, you would create an NSString property. You can assign a value to this property anytime after you alloc/init your view controller. Then in your viewDidLoad, you would set the text of your UITextView to this NSString property.
Correct. For example, if you wanted to set the text in your UITextView, you would create an NSString property. You can assign a value to this property anytime after you alloc/init your view controller. Then in your viewDidLoad, you would set the text of your UITextView to this NSString property.
Got it...thanks for the help. I'm still surprised at the behavior of initWithNibName in this case. It's a misnomer, since it doesn't actually init your view controller with any of the nib elements.
Actually, it does exactly what it say. It initializes the view controller, but not the view. Those are two very different objects with very different purposes when it comes to the Modal-View-Controller architecture. In fact, the view controller generally has a longer lifespan than its view, and can often be loaded into memory without the view. For example, if you present a new modal view controller, the view controller "below" and its view are no longer in effect or visible. In this case, if memory becomes low, iOS can and will unload the underlying view to free memory, but the view controller will still remain loaded. When you dismiss the modal VC, the old VC is about to become visible again, so it loads it's view again, and again all the methods (viewWillLoad, didLoad, willAppear, didAppear) are called in order, but not the init method since the controller is already in memory.
I strongly suggest you read the View Controller Programming Guide from the documentation, it explains all these little intricacies and how everything works, including different types of controllers (navigation, tab, modal, split etc).