开发者

NSInvalidArgumentException when posting notification

开发者 https://www.devze.com 2023-03-02 02:44 出处:网络
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:

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 开发者_StackOverflowto be used to push in.


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

精彩评论

暂无评论...
验证码 换一张
取 消