0

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.

bryanmac
  • 38,941
  • 11
  • 91
  • 99
Piero
  • 9,173
  • 18
  • 90
  • 160
  • post more code...give whole code for the first function you gave..and when that is called.. – Shubhank Mar 19 '12 at 11:04
  • i have edited...but the NSLog work, so i can't understand why don't change the text in the label...also if i do this: mySerialTitle.text = @"CIAO"; in the configure view method or in setDetailItem method, don't change the text...i'm going crazy – Piero Mar 19 '12 at 11:21
  • add breakpoint and check if the configure view is actually called or not... also can you tell when exactly you call the first function .. give the code from where you call it..post that function too.. – Shubhank Mar 19 '12 at 11:29
  • yes enter in the configure method, i sad that the NSLog work and show me my string in the console, and also the breakpoint work, all work, but the IBOutlets don't work...work only in the viewdidload and viewwillappear – Piero Mar 19 '12 at 11:35
  • i have edited the call function as you ask me – Piero Mar 19 '12 at 11:48
  • You still don't cover when setDetailItem is called. The key question is whether it's called *before* viewDidLoad is called. If that's the case, it's expected. – bryanmac Mar 19 '12 at 11:53
  • When you are debugging "configureView" are you sure mySerialTitle is not nil? – El Guapo Mar 19 '12 at 11:56

2 Answers2

0

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:

This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.

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.

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

If you want to set the data on the view you're pushing, you have a couple options.

  1. 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.

  2. 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?

Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • i know when the viewdidload and viewwillappear is called, my problem is that i can't set the Text of my uilabel, in my code, and works only in the viewwillappear and viewdidload method, – Piero Mar 19 '12 at 11:41
  • i have write the Console Log, and seems that it's called first of the viewdidload, it's there the problem? – Piero Mar 19 '12 at 11:57
  • If you're code is called first and then viewDidLoad is called (after), then yes, that's a problem. You cannot manipulate the views before they are loaded. – bryanmac Mar 19 '12 at 12:00
  • yes it's that the problem, so how i can use the setDetailitem method to pass the attribute to the other view? – Piero Mar 19 '12 at 12:02
  • I appended the answer with your options. – bryanmac Mar 19 '12 at 12:09
  • i put the information in Viewwillappear and works! thanks to all! – Piero Mar 19 '12 at 12:11
0
- (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];
    }

  }
 }

the above function wherever you are calling it form..i don't know.. make sure you call it after

  [self.navigationController pushViewController:self.searchResultViewController animated:YES];

if you call it before this.. then your view has not loaded properly and setting text to a label won't work..

Shubhank
  • 21,721
  • 8
  • 65
  • 83