iphone - How to save tab bar order when app is terminated? -
the following example: http://www.raddonline.com/blogs/geek-journal/iphone-sdk-uitabbarcontroller-how-to-save-user-customized-tab-order/
doesn't work in ios4.
i able save order of tabs when application shut down.
you can set application delegate uitabbarcontroller
delegate, , when tab selected, can record selection [nsuserdefaults standarddefaults]
integer (selected index of tab bar controller).
do in:
- (void)tabbarcontroller:(uitabbarcontroller *)tabbarcontroller didselectviewcontroller:(uiviewcontroller *)viewcontroller; { [[nsuserdefaults standarddefaults] setinteger:tabbarcontroller.selectedindex forkey:selectedtabindex]; }
on launch, can load selected index defaults using -integerforkey:
, default 0 if value not found key, or if key not parsed integer.
i wrap access saved index method:
- (nsuinteger)tabindextoselect; { return [[nsuserdefaults standarddefaults] integerforkey:selectedtabindex]; // defaults 0 }
a note nsuserdefaults
.. until syncronized, not flushed disk persistence. notice if you're debugging , terminate app, , ios hasn't automatically synchronized app's defaults. can force synchronize, prefer leave until it's done automatically. cases user switches tab, , quits, add synchronize - (void)applicationwillresignactive:(uiapplication *)application
and/or - (void)applicationwillterminate:(uiapplication *)application
.
referring tab order, rather selected index (my bad!).
you can set each of tab bar items unique tag number. (use enumeration in app delegate keep them unique). can this:
- (void)tabbarcontroller:(uitabbarcontroller *)tabbarcontroller didendcustomizingviewcontrollers:(nsarray *)viewcontrollers changed:(bool)changed; { nsarray *tabbartagorder = [tabbarcontroller.tabbar.items valueforkey:@"tag"]; [[nsuserdefaults standarddefaults] setobject:tabbartagorder forkey:tabtagorder]; }
to restore order @ launch, can manually organize tab bar items in order restored defaults, , set items
property of uitabbar
directly.
Comments
Post a Comment