Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-07-2011, 01:30 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 12
josel_dev is on a distinguished road
Default 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?
josel_dev is offline   Reply With Quote
Old 09-07-2011, 02:17 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

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.
baja_yu is offline   Reply With Quote
Old 09-07-2011, 02:40 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 12
josel_dev is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
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.
josel_dev is offline   Reply With Quote
Old 09-07-2011, 02:46 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

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.
smithdale87 is offline   Reply With Quote
Old 09-07-2011, 03:27 PM   #5 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 12
josel_dev is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
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.
josel_dev is offline   Reply With Quote
Old 09-07-2011, 03:29 PM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

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.
smithdale87 is offline   Reply With Quote
Old 09-07-2011, 03:35 PM   #7 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 12
josel_dev is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
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.

Last edited by josel_dev; 09-07-2011 at 03:44 PM.
josel_dev is offline   Reply With Quote
Old 09-07-2011, 03:43 PM   #8 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 12
josel_dev is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
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.
josel_dev is offline   Reply With Quote
Old 09-07-2011, 04:46 PM   #9 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

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.
smithdale87 is offline   Reply With Quote
Old 09-07-2011, 05:25 PM   #10 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 12
josel_dev is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
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.
josel_dev is offline   Reply With Quote
Old 09-07-2011, 05:42 PM   #11 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

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).
baja_yu is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 372
8 members and 364 guests
apatsufas, JackReidy, jeroenkeij, Sami Gh, tim0504, UMAD, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:54 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0