在 uinavigation 的视图之间传递信息
我正在实现一个 uinavigationcontroller。第一个视图是带有姓名列表的 uitableview(想象一下联系人应用程序)。 第二个视图是人员概况。 因此,当我单击应用程序中的某个人时,它应该加载他的个人资料。
如何将人员数据传递到第二个视图?
在 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。
这里有什么问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而不是:
您可以简单地使用:
这将利用
person
属性设置器并将给定对象分配为varContactView
的数据。该设置器的默认实现是(在retain
属性的情况下):这就是您试图在
-initWithPerson:
方法中实现的目标。不需要该方法,因为它的功能由person
属性设置器涵盖。顺便说一句,请记住在视图控制器的-dealloc
方法中释放person
属性:但是,错误的访问异常可能是由代码中的其他内容引起的......
Instead of:
you can simply use:
That will make use of
person
property setter and assign the given object asvarContactView
's data. The default implementation of that setter is (in case ofretain
property):That's what you're trying to achieve in
-initWithPerson:
method. That method is not needed, as its functionality is covered byperson
property setter. BTW, remember to releaseperson
property in-dealloc
method of your view controller:The bad access exception might be caused by something else in your code, though...
在 .m 文件中,更改代码如下。
In .m file, change the code as given below.