views:

144

answers:

2

I'm creating an instance of a viewController, and then trying to set the text on of it's properties, a UILabel.

BoyController *boyViewController = [[BoyController alloc] initWithNibName:@"BoyView" bundle:nil];
     NSString *newText = [astrology getSignWithMonth:month withDay:day];
     boyViewController.sign.text = newText;
     NSLog(@" the boyviewcontroller.sign.text is now set to: %@", boyViewController.sign.text);
     [newText release];

I tried this, but it didn't work...

So I tried the following:

BoyController *boyViewController = [[BoyController alloc] initWithNibName:@"BoyView" bundle:nil];
 UILabel *newUILabel = [[UILabel alloc] init];
 newUILabel.text = [astrology getSignWithMonth:month withDay:day];
 boyViewController.sign = newUILabel;
 NSLog(@" the boyviewcontroller.sign.text is now set to: %@", newUILabel.text);
 [newUILabel release];

But no avail..

I'm not sure why I can't set the text property of the UILabel "sign" in boyViewController..

+1  A: 

Did you bind your outlets at Interface Builder?

It seems that you need to bind sign outlet of the first example into Interface Builder in order to actually set that text to whatever you want.

Once you bind your outlet to the actual UI component at Interface Builder, then you should be able to do something like:

NSString *newText = [astrology getSignWithMonth:month withDay:day];
[[boyViewController sign] setText:newText];

This is what you need to know about binding.

Your second example does not make sense at all to me.

Pablo Santa Cruz
+1  A: 

The problem here is that the initializer does not actually load the nib file into memory. Instead, loading the nib is delayed until your application requests the view controller's view property. As such, your controller's sign property is null when you access it.

Manually requesting the controller's view property would make your example work...

BoyController *boyViewController = [[BoyController alloc] initWithNibName:@"BoyView" bundle:nil];

[boyViewController view]; // !!!: Calling [... view] here forces the nib to load.

NSString *newText = [astrology getSignWithMonth:month   withDay:day];
boyViewController.sign.text = newText;
// and so on...

However, I'd guess that what you're really trying to do is create and configure your view controller before setting it free to do it's own thing. (Perhaps to display it modally, say.) Calling [... view] manually is not going to be a long-term solution.

Better is to set a separate property on your view controller for the label text and then implement viewDidLoad to assign it to the label:

@interface BoyViewController : UIViewController {
    IBOutlet UILabel *label;
    NSString *labelText;
}
@property(nonatomic, copy)NSString *labelText;
@end

@implementation
@synthesize labelText;

- (void)viewDidLoad
{
    [label setText:[self labelText]];
}

// and so on...

@end

This has the added benefit of your label text being reset in case the view is purged during a low memory event.

Carlton Gibson