在uinavigation中传递视图之间的信息

时间:2011-10-24 11:09:53

标签: ios uinavigationcontroller

我正在实施一个uinavigationcontroller。第一个视图是一个uitableview(想象一下Contacts应用程序),其中包含一个名称列表。 第二个视图是人物简介。 所以,当我点击uitable中的某个人时,就会加载他的个人资料。

如何将人员数据传递到第二个视图?

在didSelectRowAtIndexPath中我做:

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];

在ContactView的界面中我得到了:

Person * person;

然后:

@property (nonatomic, retain) Person * person;
-(void) initWithPerson:(Person *)newperson;

在.m:

@synthesize person
-(void) initWithPerson:(Person *)newperson{
    person = [[Person alloc] init];
    person = newperson;
}

但是,当我尝试访问ContactView中的人时,它总是说EXC_BAD_ACCESS。

这里有什么问题?

2 个答案:

答案 0 :(得分:2)

而不是:

[varContactView initWithPerson:[contactsArray objectAtIndex:indexPath.row]];

你可以简单地使用:

varContactView.person = [contactsArray objectAtIndex:indexPath.row];

这将使用person属性设置器并将给定对象指定为varContactView的数据。该setter的默认实现是(在retain属性的情况下):

- (void)setPerson:(Person *)newPerson
{
    if (person != newPerson) {
        [person release];
        person = [newPerson retain];
    }
}

这就是您在-initWithPerson:方法中尝试实现的目标。不需要该方法,因为person属性设置器涵盖了其功能。顺便说一句,记得在视图控制器的person方法中发布-dealloc属性:

- (void)dealloc
{
    [person release];
    [super dealloc];
}

错误的访问异常可能是由代码中的其他内容引起的,但是......

答案 1 :(得分:0)

在.m文件中,更改下面给出的代码。

-(void) initWithPerson:(Person *)newperson
{
    self.person = newperson;
}