在 2 个视图控制器之间传递对象
我试图在 2 个 VC 之间传递一个对象,从弹出窗口到分割视图控制器的详细视图。
我想我需要使用 NSNotificationCenter。
我尝试过这个,但似乎无法让它发挥作用。
在popover的didSelectRow
中
[[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" withObject:objectToPass];
在detail VC
中
- (void) didReceiveNotificationPassObject:(NSNotification*)notification
{
YourObjectClass *theObject = (YourObjectClass*)notification.object;
}
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotificationPassObject:) name:@"PassObject" object:nil];
}
I'm trying to pass an object between 2 VCs, from a popover to the detail view of split view controller.
I think I need to use NSNotificationCenter.
I tried this but can't seem to get it to work.
In didSelectRow
of popover
[[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" withObject:objectToPass];
In detail VC
- (void) didReceiveNotificationPassObject:(NSNotification*)notification
{
YourObjectClass *theObject = (YourObjectClass*)notification.object;
}
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotificationPassObject:) name:@"PassObject" object:nil];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能只是输入问题时的拼写错误,但在您发布通知的第一行中,
方法签名是错误的 - 它应该是“object:objectToPass”而不是“withObject:objectToPass”。您所在的行将在编译时发出警告并在运行时崩溃。
除此之外,所有逻辑似乎都很好。
Probably just a typo when entering the question but in the first line where you post the notification
the method signature is wrong - it should be 'object:objectToPass' not 'withObject:objectToPass'. The line you have there will compile with a warning and crash at runtime.
Aside from that all the logic seems fine.
您面临的问题是什么?
didReceiveNotificationPassObject:
命中了吗?如果没有,您可以验证viewDidLoad
是否在didSelectRow
之前执行。使用
[[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" object:objectToPass];
而不是[[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" withObject:objectToPass];
另外,不要忘记在
viewDidUnload
中removeObserver
。HTH,
阿克谢
What is the problem you are facing? Does
didReceiveNotificationPassObject:
hit? If it doesn't, you could verify thatviewDidLoad
executes beforedidSelectRow
.Use
[[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" object:objectToPass];
instead of[[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" withObject:objectToPass];
Also, don't forget to
removeObserver
inviewDidUnload
.HTH,
Akshay
使用多个参数进行通知的一种快速而简单的解决方案是像这样调用通知
,其中“相机”就像您的参数一样。然后
A fast and easy solution to notify with multiple parameters is to call the notification it like this
Where "camera" acts like your parameter. Then