iPhone: how to reload tableView and push a detailView from AppDelegate?
my app is nav based. in which i have a main tableView which shows feed items in cells. when a cell is clicked, a detailview is created which shows details of that feed item. i am working with push notifications now. when action button in notification is clicked,
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo:
is called. how can i implement that method that if action button in notification is clicked. it should parse the feed again, reload the tableview, creates the latest feed items detailview and push it in navigational stack. i tried some code but it didn't work. here is the code i wrote.
in AppDelegate:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
RootViewController *controller = [[RootViewCo开发者_开发知识库ntroller alloc] init];
[controller newFeedItem];
}
in RootViewController:
- (void)newFeedItem
{
spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame=CGRectMake(130, 170, 50, 50);
[self.view addSubview:spinner];
[spinner startAnimating];
[self performSelector:@selector(doStuff) withObject:nil afterDelay:0.01];
}
-(void)doStuff
{
[[self stories] removeAllObjects];
[self startParsing];
[self.tableView reloadData];
// create detailview and push it in navigational stack
[spinner stopAnimating];
[spinner release];
}
but activity indicator is not appearing and tableView is also not reloading. Why is it happening so? Thanx in advance
Not quite sure about your program flow but I assume you show the rootViewController at program start and at a later time you receive a remote notification.
In your code (in didReceiveRemoteNotification
) you are instantiating a new rootViewController and that new one will be different from the one already on the screen. Following your approach you might want alloc the controller once and keep it around for later when the remote notification arrives.
Personally I would suggest to use local notifications and to fire a local notification in didReceiveRemoteNotification
and catch it in the rootViewController. That would ensure the currently active instance of the controller responds.
Also not sure why the spinner not shows, for a try call it from the viewDidAppear
, just it see it it works at all and if the problem is with the reaction to the remote notification. And use some breakpoints.
Edit with respect to your comment:
in the interface define
RootViewController *controller
in the implementation you alloc the controller (for example in appDidFininshLaunching
)
if (controller == nil) controller = [[RootViewController alloc] init]
in didReceiveRemoteNotification
you can then do
[controller newFeedItem];
without alloc'ing it again and you can refer to the same controller. Don't forget to release it in -(void)dealloc
精彩评论