发布通知时出现 NSInvalidArgumentException

发布于 2024-11-04 21:56:53 字数 925 浏览 4 评论 0原文

我正在尝试使用通知系统,以便在 Splitviewcontroller 中拥有详细视图来更新表视图。我声明通知如下:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushView:) name:@"pushView" object:nil];

和选择器本身:

- (void) pushView:(UIViewController *) viewController {
    [self.navigationController pushViewController:viewController animated:YES];
}

现在,在详细视图中,我创建视图控制器并调用创建通知:

   ArticleTableViewController *articleTableView  = [[ArticleTableViewController alloc] initWithCategory:catInt];

   [[NSNotificationCenter defaultCenter] postNotificationName:@"pushView" object:articleTableView];

我认为这会起作用,但我收到错误:

* 由于未捕获而终止应用程序 例外 'NSInvalidArgumentException',原因: '-[NSConcreteNotification 设置ParentViewController:]: 无法识别的选择器发送到实例 0x5a3a290'

所以我想我在如何将detailViewController包含在用于推送的通知中做错了。

I am trying to use the notification system in order to have a detail view in a Splitviewcontroller to update the tableview. I declared the notification as follows:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushView:) name:@"pushView" object:nil];

and the selector itself:

- (void) pushView:(UIViewController *) viewController {
    [self.navigationController pushViewController:viewController animated:YES];
}

Now, in the detailview I create the view-controller and call create the notification:

   ArticleTableViewController *articleTableView  = [[ArticleTableViewController alloc] initWithCategory:catInt];

   [[NSNotificationCenter defaultCenter] postNotificationName:@"pushView" object:articleTableView];

I assumed that that would work, but I get the error:

* Terminating app due to uncaught
exception
'NSInvalidArgumentException', reason:
'-[NSConcreteNotification
setParentViewController:]:
unrecognized selector sent to instance
0x5a3a290'

So I guess I am doing something wrong in how including the detailViewController in the notification to be used to push in.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

猫腻 2024-11-11 21:56:54

处理通知的方法定义似乎是错误的。

- (void) pushView:(UIViewController *) viewController

应该是,

- (void) pushView:(NSNotification *) notification

实际的通知作为参数传递,而不是任何视图控制器。要实现您想要的目标,请尝试以下操作。

- (void) pushView:(NSNotification *) notification

    NSDictionary *userInfo = [notification userInfo];
    UIViewController *viewController = (UIViewController *)[userInfo objectForKey:@"ViewController"];
    [self.navigationController pushViewController:viewController animated:YES];
}

并且在发布通知的同时,

    ArticleTableViewController *articleTableView  = [[ArticleTableViewController alloc] initWithCategory:catInt];
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:articleTableView forKey:@"ViewController"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushView" object:nil userInfo:userInfo];

The method definition for handling the notification seems to be wrong.

- (void) pushView:(UIViewController *) viewController

should be,

- (void) pushView:(NSNotification *) notification

The actual notification is passed as the argument, not any view controllers. To achieve what you want, try the following.

- (void) pushView:(NSNotification *) notification

    NSDictionary *userInfo = [notification userInfo];
    UIViewController *viewController = (UIViewController *)[userInfo objectForKey:@"ViewController"];
    [self.navigationController pushViewController:viewController animated:YES];
}

And while posting the notification,

    ArticleTableViewController *articleTableView  = [[ArticleTableViewController alloc] initWithCategory:catInt];
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:articleTableView forKey:@"ViewController"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushView" object:nil userInfo:userInfo];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文