iOS: Changing UIButton Title through Delegate -
i'm using delegation change title of uibutton.
.h mainview
//mainviewcontroller.h #import <uikit/uikit.h> @class signupdelegate; @protocol signupdelegate <nsobject> @required -(void)loggedin; @end @interface mainviewcontroller : uitableviewcontroller <nsfetchedresultscontrollerdelegate> { id <signupdelegate> delegate; } @property (nonatomic, assign) id <signupdelegate> delegate; -(void)loggedin; @end
.m
@interface mainviewcontroller () //this button connected uinavigationbar button needs title changed. //via interface builder, default value of title setup "login" -@property (weak, nonatomic) iboutlet uibarbuttonitem *loginoutbutton; @end -(void)loggedin { nslog (@"this logged in inside mainview.m"); self.loginoutbutton.title = @"logout"; } - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { uiviewcontroller *destinationviewcontroller = segue.destinationviewcontroller; signup *signup = [destinationviewcontroller iskindofclass:[signup class]] ? (signup*)destinationviewcontroller : nil; signup.mainviewcontroller = self.delegate; }
.h signup
#import <uikit/uikit.h> #import "mainviewcontroller.h" @interface signup : uiviewcontroller <uitextfielddelegate, uiactionsheetdelegate, signupdelegate> @property (strong, nonatomic) mainviewcontroller *mainviewcontroller; @end
.m
@synthesize mainviewcontroller; - (ibaction)createuser:(id)sender { [self loggedin]; } - (void) loggedin { nslog (@"this logged in inside signup"); [mainviewcontroller loggedin]; }
so, both nslogs print fine, think means delegate working, however, title on uibutton on navigation bar never changes "logout"
that's because recreate stmasterviewcontroller
(should have been mainviewcontroller
instead?) every time in loggedin
delegate method. (you can verify adding breakpoint on -[mainviewcontroller loggedin]
, checking if self.loginoutbutton
non-nil). instead should reference existing instance of mainviewcontroller
, operate on that.
Comments
Post a Comment