0

I have the following class:

@interface DiscountDetailViewController : UIViewController {
    UILabel * titleLabel;
    UILabel * offerLabel;
}

@property (nonatomic, retain) IBOutlet UILabel * titleLabel;
@property (nonatomic, retain) IBOutlet UILabel * offerLabel;

@end

and I tried to do the following in the previous view:

discount = [[DiscountDetailViewController alloc] initWithNibName:@"DiscountDetailViewController" bundle:nil];
discount.titleLabel.text = temp.vendor;
discount.offerLabel.text = temp.description;
[self.navigationController pushViewController:discount animated:YES];

The issue is that, discount.titleLabel.text when printed is always null... I think it's because I define the titleLabel using interface builder.. so is there a way to resolve this?

I've hooked it up with IB as well..

2
  • Where is "temp" defined? Commented Apr 19, 2011 at 19:28
  • It's there somewhere, when I do NSLog(@"%@", temp.vendor), it prints out just fine.. Commented Apr 19, 2011 at 19:29

2 Answers 2

1

i don't believe the iboutlets get hooked up until the view is on screen for the first time.

you could try setting the label after its displayed, or add another property to store the label text, then set the iboutlet label based on this new property in viewDidLoad in your DiscountDetailViewController.

Sign up to request clarification or add additional context in comments.

1 Comment

More specifically, outlets are connected when the view controller's -viewDidLoad is called, and that happens when the controller's view property is first accessed. You should usually avoid accessing that property until after -viewDidLoad in order to support lazy loading of the view, but the view may or may not end up on the screen when it's loaded.
0

If your outlets are properly connected then it is only because your titleLabel will not be available until your viewIsLoaded around -(void)viewDidLoad, I recommend you use an NSString property that can set the title label once view is loaded and update it when it is changed (override setter), or if you know your view is about to show try calling view first.

discount = [[DiscountDetailViewController alloc] initWithNibName:@"DiscountDetailViewController" bundle:nil];
//Force view to load, do not really recommend (though for some reason I am showing you how)
[discount view];
discount.titleLabel.text = temp.vendor;
discount.offerLabel.text = temp.description;
[self.navigationController pushViewController:discount animated:YES];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.