How can I "reset" the tabbar in an iPhone application -
i've iphone application: when open app see "loginview". if login application see tabbarcontroller. in third , last tab there "logout" button. if click see "loginview" again. problem if login again see "old" tabbar , selected tab third , not one, , there "logout" button. also, if user login different user, see old data of previous user (very dangerous).
here's code: - delegate.h:
uitabbarcontroller *tabbarcontroller; loginviewcontroller *loginview; - delegate.m (didfinishlaunchingwithoptions):
[self.window makekeyandvisible]; loginview = [[loginviewcontroller alloc] init]; if (yes) { /* if user not logged */ [self.window addsubview:loginview.view]; } delegate.m (methods):
- (void)logincomplete { [loginview dismissmodalviewcontrolleranimated:yes]; [window addsubview:tabbarcontroller.view]; } - (void)logoutcomplete { [[tabbarcontroller view] removefromsuperview]; [tabbarcontroller release]; [window addsubview:loginview.view]; } and here's 2 methods in 2 different viewcontrollers:
- (ibaction)login:(id)sender { tabnavisappdelegate *delegate = (tabnavisappdelegate *) [[uiapplication sharedapplication] delegate]; [delegate logincomplete]; } (the logout method same)
guys, how can solve painful problem? so, here's list of application want: "foursquare", "brightkite" , others. each 1 have login screen, tabbar view , logout button.
thanks @ everyone.
for login-logout-login situations kinds of things need reset @ logout or next login, create notification, "newuserreset." needs reset original state listens notification , runs method whatever kind of resetting needs. tabbar change button title logout, temporary data structures nil/zero/release themselves, etc.
it's nicely decouples logout of things have done you're not trying manipulate view controllers , data storage , view appearances the controller received logout tap.
sending notification easy. when user taps logout button you'll send out notification this:
[[nsnotificationcenter defaultcenter] postnotificationname:@"jmuserlogout" object:nil]; you don't have call jmuserlogout, need string you'll recognize , -- used initials -- ensure don't accidentally send notification has same name notification you're unaware of listening for.
when notification goes out, object has registered defaultcenter listen @"jmuserlogout" perform actions choose. here's how object registers (this should located in place viewwillload or initialization method of object):
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(resetfornewuser:) name:@"jmuserlogout" object:nil]; the selector there, resetfornewuser:, name of method want run when notification goes out. method looks this:
- (void)resetfornewuser:(nsnotification *)notif { // here } where says // here you'll add code specific app. example, can add tab bar observer of jmuserlogout notification. in resetfornewuser: method you'd change name of logout button login.
in viewcontroller or view or data store holds old data previous user resetfornewuser method delete of data , set things way should fore new user. example, if previous user entered data uitextfield delete text, yourtextfieldname.text = @"";
lastly, it's important remove object observer before it's deallocated. in dealloc method of each object registered receive notification add this:
[[nsnotificationcenter defaultcenter] removeobserver:self]; hopefully makes sense. apple documentation nsnotificationcenter explains more , provide several sample apps use notifications.
Comments
Post a Comment