I’ve done this many times with code that is exactly the same, but for some reason it isn’t working today.
ExampleViewController1 *exampleView = [[ExampleViewController1 alloc] initWithNibName:@"ExampleViewController1" bundle:nil];
[exampleView setProjectName:[[self.projectListArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
NSLog(@"%@", [[self.projectListArray objectAtIndex:indexPath.row] objectForKey:@"name"]);
XAppDelegate.stackController pushViewController:exampleView fromViewController:nil animated:YES]
My NSLog prints out appropriately.
My ExampleViewController1.h file declared like:
@property(nonatomic, strong) NSString *projectName;
I then do this code in ExampleViewController1.m‘s
-(void)viewDidLoad {
NSLog(@"%@", self.projectName);
self.projectNameLabel.text = self.projectName;
[super viewDidLoad];
}
The results of my NSLogs are curious. The NSLog from my viewDidLoad appears to be getting called before my other one:
2012-04-22 10:59:41.462 StackedViewKit[43799:f803] (null)
2012-04-22 10:59:41.463 StackedViewKit[43799:f803] NewTest
I have confirmed that the (null) value there is from NSLog(@"%@", self.projectName);, but that should be the second NSLog called…I can’t figure out why it is coming through first.
Someone requested this code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// random color
self.view.backgroundColor = [UIColor colorWithRed:((float)rand())/RAND_MAX green:((float)rand())/RAND_MAX blue:((float)rand())/RAND_MAX alpha:1.0];
}
return self;
}
As I expected, the problem is that you are trying to access
self.viewinside the initialization method. So move the lineself.view.backgroundColor = ...to theviewDidLoadmethod:In fact, the documentation of the
viewproperty says:So when you call
self.viewin the initialization method, the view controller will have to load the view (from the nib or using theloadViewmethod). And that’s whyviewDidLoadis called.