tags:

views:

240

answers:

1

Hi everybody. I'm a beginneri iPhone developer. My code is the following:

   - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        displayLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, 230, 40)];
        [self.view addSubview:displayLabel];
    }
    return self;
   }

//
- (void)viewDidLoad {
    [super viewDidLoad];
    displayLabel.text = @"abc";
}

I want to change the label text in viewDidLoad methond,but it can't change label text.

I hope i explained my scenario. Sorry for my English. Greetings, dy.

+1  A: 

You should try adding the subview in viewDidLoad.

self.view is an IBOutlet and it will not be loaded into memory at the point that initWithNibName is called, and will be equal to nil.

viewDidLoad is called after the view controller has loaded its associated views into memory. And as such self.view will point to the view you created in your nib file.

Alan Rogers
- (void)viewWillAppear { displayLabel.text = @"abc";}This is ok!
HelloWorld