Open specific view on receiving Push Notification
I have UITableView
as my rootViewController
and I populate that table with parsed RSS (there's a Parser
class where my rootViewController
is its delegate). In the rootViewController
I have methods for refreshing RSS refreshData
and I keep my retreived data in a static MutableArray
staticItems:
On clicking a cell in tableView
cell the detailView
gets pushed on the navigationController
while at the same time (on selecting the cell (row)) I create and pass a dictionary theItem
to the detailView
. In that dictionary I pass the values from staticItems
and the positionInArray
(index of selected cell). This way I can show the text of the news and keep track about the position of the news in the array of news to implement slide prev/next.
Now, I enabled push notifications and on receiving one my app is back to foreground but with the view that was opened last time when app was closed.
I would like to present t开发者_高级运维he last news in detailView by re-parsing (refreshing) the RSS and presenting the last news (theItem[0]).
So, I would like to have the result of the following: calling the [rootController refreshData]
and then selecting the first item in the cell and opening it in detailView
I've been playing with the delegate method didReceiveRemoteNotification
, but I can't find the way of making it work. I tried creating new rootController
, but then I stack it over existing one :(.
Please share your thoughts with me :)
First of all, this question is not really related to push notifications at all. It's more a question of how to access your view controllers from an arbitrary place in your application delegate.
Your best (and possibly only) bet is to manually keep references to the relevant view controller instances.
I'm assuming you use a UINavigationController
where the root is your list, and then you push a detail view controller onto it. Keep a reference to this navigation controller in your app delegate. Add a @property (nonatomic, retain) UINavigationController *mainNavController;
to your application delegate. When you create the navigation controller, assign it so the app delegate has a reference.
MyAppDelegate *ad = ((MyAppDelegate *)[UIApplication sharedApplication].delegate);
ad.mainNavController = theNavController;
If you create the navigation controller in the app delegate itself, you obviously only need to do this:
self.mainNavController = theNavController;
Then when you receive a push notification, simply act on the navigation controller directly.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Do whatever you need to do in order to create an instance of your
// detail view controller
MyDetailViewController *vc = [MyDetailViewController magicalStuff:userInfo];
// Add the detail view controller to the stack, but keep the root view
// controller.
UIViewController *root = self.mainNavController.topViewController;
NSArray *vcs = [NSArray arrayWithObjects:root, vc, nil];
[self.mainNavController setViewControllers:vcs animated:YES];
}
Then the navigation controller will animate to MyDetailViewController
with a swipe and the back button will take you to the list.
精彩评论