i have a problem, i want set text of a UILabel or UItextView or another IBOUTLET objects,but i can do it only in the viewwillappear and viewdidload method, if i set text in another method in the code don’t change nothing, here is an example:
i have a method that retrieve string from another class, and then i want set this string in my uilabel:
- (void)setDetailItem:(id)newDetailItem
{
if (detailItem != newDetailItem) {
NSLog(@"SetDetailItem");
[detailItem release];
detailItem = [newDetailItem retain];
detailString = [[FindStringClass alloc] init];
// Update the view.
detailItem = [detailString searchStringFor:detailItem.name];
if (detailItem) {
//
NSLog(@"setDetail: %@",detailItem.stringName);
[self configureView];
}
}
}
- (void)configureView {
NSLog(@"configure view: %@",detailItem.stringName);
mySerialTitle.text = detailItem.stringName;
}
the NSLog work, and i can see my string in the console, but the text in view don’t change, instead if i set a simple text in the ViewWillAppear or viewDidLoad method work, so the connection with the IBOOutlets are right, like this:
- (void)viewDidLoad
{
mySerialTitle.text = @"CIAO";
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
This is the call function where i call the setDeatItem from another view:
self.searchResultViewController.detailItem = [myArray objectAtIndex:[indexPath row]];
[self.navigationController pushViewController:self.searchResultViewController animated:YES];
anyone can help me?
EDIT:
This is the NSLog where the method are called:
2012-03-19 12:51:02.617 TestApp[292:b603] SetDetailItem
2012-03-19 12:51:06.161 TestApp[292:b603] setDetail: Chicken
2012-03-19 12:51:06.162 TestApp[292:b603] configure view: Chicken
2012-03-19 12:51:06.175 TestApp[292:b603] View DidLoad
i call the searchviewcontroller view from a Tableviewcontroller i press on a row and then call the new view and pass the attribute.
viewDidLoad is called after the view is loaded into memory. See viewDidLoad in the apple docs. After the view is loaded into memory and right before it appears, viewWillAppear is called.
You cannot change the properties of the view before it is loaded. So, examine your code paths (and maybe even log out in those methods and the viewDidLoad and viewWillAppear method) and see if you’re setting those properties before viewDidLoad is called (log output will make it easy to see).
From viewDidLoad docs:
You’re log statements that you added makes it clear that you are manipulating the views before they are loaded. As you can see, setDetail is called before viewDidLoad.
If you want to set the data on the view you’re pushing, you have a couple options.
Call setDetail but in setDetail only set iVar data – don’t manipulate views. Then, in viewDidLoad/WillAppear, read the iVar data and manipulate the views.
Use delegates to have the view you’re pushing call back to the launching view. See What exactly does delegate do in xcode ios project?