I’m implementing a uinavigationcontroller. The first view is a uitableview (imagine the Contacts app) with a list of names.
The second view is the person profile.
So, when I click a person in the uitable, it’s suppose to load his profile.
How do I pass the person data to the second view?
In didSelectRowAtIndexPath I do:
ContactView * varContactView = [[ContactView alloc] initWithNibName:nil bundle:nil];
varContactView.title = [[contactsArray objectAtIndex:indexPath.row] name];
[varContactView initWithPerson:[contactsArray objectAtIndex:indexPath.row]];
[navigationController pushViewController:varContactView animated:YES];
In the interface of ContactView I’ve got:
Person * person;
And then:
@property (nonatomic, retain) Person * person;
-(void) initWithPerson:(Person *)newperson;
And in .m:
@synthesize person
-(void) initWithPerson:(Person *)newperson{
person = [[Person alloc] init];
person = newperson;
}
However, when I try to access person in ContactView, it says always EXC_BAD_ACCESS.
What is wrong here?
Instead of:
you can simply use:
That will make use of
personproperty setter and assign the given object asvarContactView‘s data. The default implementation of that setter is (in case ofretainproperty):That’s what you’re trying to achieve in
-initWithPerson:method. That method is not needed, as its functionality is covered bypersonproperty setter. BTW, remember to releasepersonproperty in-deallocmethod of your view controller:The bad access exception might be caused by something else in your code, though…