Reputation: 11053
When my app receives a notification it handles it like this opening a specific view:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSLog(@"Recieved Notification didFinishLaunchingWithOptions %@",localNotif);
[theLastPresentedViewController presentViewController:myVC animated:YES completion:nil];
}
Basically my question is how to get access to the last presented ViewController theLastPresentedViewController so I can present myView
on top of it ?
While the app uses a tabBarViewController as the basis, I present other viewControllers on top of other views so there is no way to know which ViewController has beed presented as the last one.
Do I have to manually remember each ViewController I present, so when the app launches I can access the last one or is there a way to directly access the last visible viewController of the app ?
Since the user can just press the HOME Button and exit the app there is no way to know which view was shown at this moment. It would also be OK to dismiss all these views from the stack until the tabBarViewController is reached.
Many thanks!
Upvotes: 0
Views: 1203
Reputation: 11053
I found this to access the top most window/view:
[[[UIApplication sharedApplication] keyWindow] addSubview:someView]
Upvotes: 1
Reputation: 3070
It's basically a bad idea to perform "random" pushes onto navigation stack. You could present a semi-modal or modal controller as a reaction to your notification, and it does not generally require the pointer to "top visible" view controller.
Alternatively, you can allow your view controllers / views to accept first responder, like:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
Now, you can send an event:
[[UIApplication sharedApplication] sendAction:@selector(popNotification) to:nil from:notificationObj forEvent:nil];
And it will be dispatched to first responder (that is any view in focus, and down to view controller, as the view cannot handle the custom event). From this point you are in a context of top visible view controller and can proceed with your original task.
Upvotes: 2
Reputation: 2506
- (void)applicationDidEnterBackground:(UIApplication *)application{
saveIndex = self.tabBarController.selectedIndex //shows the current index of the viewController of the Array of the tabBarController
//add some stuff to save the integer}
You could save this integer when the User quits the App and after the app becomes active load the tabBarController with this element of the Array
To open the saved view write this in your AppDelegate:
- (void)applicationDidBecomeActive:(UIApplication *)application{
[self.tabBarController setSelectedIndex:savedIndex];}
This should do it
Upvotes: 0