在 2 个视图控制器之间传递对象

发布于 2024-12-01 06:02:16 字数 692 浏览 0 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

記憶穿過時間隧道 2024-12-08 06:02:16

可能只是输入问题时的拼写错误,但在您发布通知的第一行中,

[[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" withObject:objectToPass];

方法签名是错误的 - 它应该是“object:objectToPass”而不是“withObject:objectToPass”。您所在的行将在编译时发出警告并在运行时崩溃。

除此之外,所有逻辑似乎都很好。

Probably just a typo when entering the question but in the first line where you post the notification

[[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" withObject:objectToPass];

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.

冷︶言冷语的世界 2024-12-08 06:02:16

您面临的问题是什么? didReceiveNotificationPassObject: 命中了吗?如果没有,您可以验证 viewDidLoad 是否在 didSelectRow 之前执行。

使用 [[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" object:objectToPass]; 而不是 [[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" withObject:objectToPass];

另外,不要忘记在viewDidUnloadremoveObserver

HTH,

阿克谢

What is the problem you are facing? Does didReceiveNotificationPassObject: hit? If it doesn't, you could verify that viewDidLoad executes before didSelectRow.

Use [[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" object:objectToPass]; instead of [[NSNotificationCenter defaultCenter] postNotificationName:@"PassObject" withObject:objectToPass];

Also, don't forget to removeObserver in viewDidUnload.

HTH,

Akshay

已下线请稍等 2024-12-08 06:02:16

使用多个参数进行通知的一种快速而简单的解决方案是像这样调用通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"shareButton" object:@"camera"];

,其中“相机”就像您的参数一样。然后

- (void)shareButton:(id)sender
{
   NSString *kindOf = [sender object];

    if ([kindOf isEqualToString:@"camera"]) {
         // Your code goes here
    }
 }

A fast and easy solution to notify with multiple parameters is to call the notification it like this

[[NSNotificationCenter defaultCenter] postNotificationName:@"shareButton" object:@"camera"];

Where "camera" acts like your parameter. Then

- (void)shareButton:(id)sender
{
   NSString *kindOf = [sender object];

    if ([kindOf isEqualToString:@"camera"]) {
         // Your code goes here
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文