Make a default view when coming back into the foreground?


I noticed that when I set the local reminder notification, test it, and then tap on the reminder that it brings me back to the reminder view. Which of course isn’t a surprise / that makes sense. But it sparked me to create a challenge for myself to add a change such that the hypno view is the “default” view, that is it always goes back to the same default view when the app comes back into the foreground (i.e. upon being reminded, you can promptly hypnotize yourself without the drudgery of selecting the view yourself) . 'Should be pretty simple add something to the applicationWillEnterForeground: method to simply change the selected view controller in the UITabBarController.

So I determined that I should set the selectedViewController property in the UITabBarController. But I couldn’t figure out a way to access the UITabBarController…I couldn’t get to anything that points to it. Am I missing some easy way to do this?

Assuming I wasn’t missing something, I created properties in BNRAppDelegate.m for the UITabBarController and the other view controllers.

@interface BNRAppDelegate ()
@property (nonatomic, strong) UITabBarController *tabBarController;
@property (nonatomic, strong) BNRHypnosisViewController *hvc;
@property (nonatomic, strong) BNRReminderViewController *rvc;
@property (nonatomic, strong) BNRQuizViewController *qvc;

I then initialized them in application:didFinishLaunchingWithOptions: …just like before except I am initiating the property objects. And now as properties it is easy to do a self.tabBarController.selectedViewController = self.hvc; in one of the other instance methods. This works, and it seems like I’m not doing anything ~bad here - I think the objects should take up the same amount of memory, would still be controlled well by ARC, and are not visible outside the instance.

Can some folks comment on if this all seems sensible…or again am I missing something?


I did find what is probably a better way to do this,
instead of creating properties for all the view controllers I was able set the selectedViewController property with the below in any of the other instance methods. 'Not sure if this is great either, but eliminates the need for all the properties.

UITabBarController *tabBarController = (UITabBarController *)[self.window rootViewController]; tabBarController.selectedViewController = tabBarController.viewControllers[0];