发布通知时发生NSInvalidArgumentException

Luu*_*sen 2 notifications cocoa-touch uiviewcontroller ipad uisplitviewcontroller

我试图使用通知系统,以便在Splitviewcontroller中有一个详细视图来更新tableview.我宣布通知如下:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushView:) name:@"pushView" object:nil];
Run Code Online (Sandbox Code Playgroud)

和选择器本身:

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

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

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

   [[NSNotificationCenter defaultCenter] postNotificationName:@"pushView" object:articleTableView];
Run Code Online (Sandbox Code Playgroud)

我认为那会有效,但我收到错误:

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

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

Emp*_*ack 5

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

- (void) pushView:(UIViewController *) viewController
Run Code Online (Sandbox Code Playgroud)

应该,

- (void) pushView:(NSNotification *) notification
Run Code Online (Sandbox Code Playgroud)

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

- (void) pushView:(NSNotification *) notification

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

在发布通知时,

    ArticleTableViewController *articleTableView  = [[ArticleTableViewController alloc] initWithCategory:catInt];
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:articleTableView forKey:@"ViewController"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushView" object:nil userInfo:userInfo];
Run Code Online (Sandbox Code Playgroud)