【问题标题】:NSInvalidArgumentException when posting notification发布通知时出现 NSInvalidArgumentException
【发布时间】:2011-05-03 19:19:27
【问题描述】:

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

[[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 设置父视图控制器:]: 无法识别的选择器发送到实例 0x5a3a290'

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

【问题讨论】:

    标签: cocoa-touch uiviewcontroller notifications uisplitviewcontroller ipad


    【解决方案1】:

    处理通知的方法定义好像是错误的。

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

    【讨论】:

    • 谢谢西蒙。但是在这种情况下我将如何实现它,即我可以使用通知将视图控制器传递给 splitviewcontroller 吗?
    • 很好,它只是通知中的对象,很好用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    相关资源
    最近更新 更多