I am trying to access properties of an object (person’s firstName) which is stored in an array and assign it to labels in a seperate view Controller (SplitMethodViewController). The name value is successfully assigned here. Code snippet as below:
In the initial view controller (before displaying the modal view controller containing the UILabel):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int row = [indexPath row];
Person *thisPerson = (Person *)[self.personArray objectAtIndex:row];
SplitMethodViewController *smvc = [[SplitMethodViewController alloc]initWithNibName:nil bundle:nil];
smvc.nameLabel.text = [[NSString alloc] initWithFormat:@"%@", thisPerson.firstName];
//This lines returns the value I want, showing that assignment is working till this point
NSLog(@"The name label is %@", smvc.nameLabel.text);
[self presentModalViewController:smvc animated:YES];
[smvc release];
}
However, the values became blank when I check in the splitMethodViewController (checked in ViewDidLoad Method)
@interface SplitMethodViewController : UIViewController
{
UILabel *nameLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@end
@implementation SplitMethodViewController
@synthesize nameLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
self.nameLabel = [[UILabel alloc] init];
}
return self;
}
- (id)init
{
return [self initWithNibName:nil bundle:nil];
}
- (void)viewDidLoad
{
//name label returning nothing here.
NSLog(@"namelabel is %@",self.nameLabel.text);
[super viewDidLoad];
}
@end
I am sure I made some silly mistake somewhere. I have tried deleting all the outlets and labels and re-created just one name label and outlet. But I am still hitting this same issue.
Any help will be appreciated!
Did you actually allocate and instantiate the nameLabel and evenBillAmountLabel once you instantiate the SplitMethodViewController? In Objective-C messages (method calls) can be sent to nil (non-existant objects) without returning any errors, but also without any results.
Make sure the -init method on SplitMethodViewController looks somewhat like this:
Be sure to read about designated initializers if this is new to you and if this was related to your issue. Here’s a link to Apple’s documentation on the subject.