views:

254

answers:

2

Question 1)

I have a navigation based application where clicking on an entry opens a second view with the details of that entry.

It's working now with the following code

[self.navigationController pushViewController:descriptionController animated:YES];
[self.descriptionController.textView setText:[s description]];  
self.descriptionController.title = [s name];

But, if I switch the first and third statements, then the very first time I click on an item, the details view shows Lorem Ipsum text. After the first time, it works as expected. I thought it would be more correct to do it that way. Why would it show Lorem Ipsum for the first view, but not anything after that?

Question 2)

Additionally, my textView element is part of the details view. In my controller class for the view I have:

@property( nonatomic, retain) IBOutlet UITextView *textView;

But if I change this to

@property( nonatomic, copy) IBOutlet UITextView *textView;

Then it crashes. I wouldn't think it would crash, but I'm thinking it just creates new instances just taking up memory for nothing. Can anybody explain this?

+5  A: 

In my controller class for the view I have:

@property( nonatomic, retain) IBOutlet UITextView *textView;

But if I change this to

@property( nonatomic, copy) IBOutlet UITextView *textView;

Then it crashes.

Because UITextView doesn't conform to NSCopying.

Peter Hosey
+5  A: 

The issue is that -[UIViewController view] and all of its subviews are populated lazily, either from you NIB or programmatically in loadView. So:

//At this point descriptionController.view == nil && textView == nil
[self.navigationController pushViewController:descriptionController animated:YES];
//At this point descriptionController.view and textView are set

[self.descriptionController.textView setText:[s description]];  
self.descriptionController.title = [s name];

So if you push the swap the push (which loads the view) after the other lines"

[self.descriptionController.textView setText:[s description]];  
self.descriptionController.title = [s name];

//At this point descriptionController.view == nil && textView == nil
[self.navigationController pushViewController:descriptionController animated:YES];
//At this point descriptionController.view and textView are set

You are sending the set message to nil values (which does nothing), then loading the view with its default values.

Louis Gerbarg
I still don't understand why they are nil in the first place, because I already have the view instantiated. So I'm just modifying objects in memory which are not visible to the GUI yet.
You have the view _controller_ instantiated but the view and subviews won't be created until it's actually displayed (i.e. pushed onto the navigation controller).
Daniel Dickison
ahh thanks guys