I'm talking about saving the selected tab. When you start Apple's Clock application, for example, it always takes you to the tab you were on when you last used it. In most cases, this is a very good idea. There may be cases where a specific tab should have preference, but generally, you should bring the user back where they left off.
If you're using a tab bar controller, it's relatively simple to implement this. In viewDidLoad or applicationDidFinishLaunching:, you need to do something like this:
NSUserDefaults *defaults = ;
NSInteger whichTab = ;
tabBarController.selectedIndex = whichTab;
Then, either when the selection changes, or when the application quits, you have to store off the tab in the preferences so it'll be there next time. I prefer doing it whenever the tab selection changes because then we've captured it, even if there's a crash or phone call, but doing it when the application finishes is fine for most purposes. Here's how you reverse the process:
NSUserDefaults *defaults = ;
NSInteger whichTab = tabBarController.selectedIndex;
;
But what if you're just using a UITabBar and not a UITabBarController? That's a touch more difficult because tab bars don't work by indices. It's relatively easy to add the exact same functionality that's on the tab bar controller to the tab bar by using a category, however:
UITabBar-Index.h
UITabBar-Index.m
Once you have that, you can import this category and use the exact same code from above on the tab bar rather than the tab bar controller.
Here's an example project. It includes a functioning tab bar application that saves the tab position, and also contains the category above.
0 komentar:
Posting Komentar