简体   繁体   English

发布通知时出现 NSInvalidArgumentException

[英]NSInvalidArgumentException when posting notification

I am trying to use the notification system in order to have a detail view in a Splitviewcontroller to update the tableview.我正在尝试使用通知系统,以便在 Splitviewcontroller 中有一个详细视图来更新 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' *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSConcreteNotification setParentViewController:]:无法识别的选择器发送到实例 0x5a3a290”

So I guess I am doing something wrong in how including the detailViewController in the notification to be used to push in.所以我想我在如何将 detailViewController 包含在用于推送的通知中做错了。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM