ios - Get variable value from another void -
i started programming in xcode , need help. have 2 voids; in first void create nsstring, , in second void need value of string, don't know how obtain value.
this shortend version of .h file
//.h file -(void)viewdidload { [self actionone]; } -(ibaction)buttonclick (id):sender { [self actiontwo]; } -(void)actionone { nsstring *varstring = @"hello"; } -(void)actiontwo { nslog (@"%@", varstring); } my problem nslog's output 'null', hope can me
you have declared local variable in actionone. lives long method running. persistent storage, need ivar (that's term used in objective-c mean "instance variable", variable there separate copy each object instance of class). easiest way declare property, create ivar , methods access reading , writing. in .h file:
@interface myviewcontroller: uiviewcontroller @property (nonatomic, strong) nsstring *varstring; // ... method declarations, etc @end and in .m file:
-(void)actionone { self.varstring = @"hello"; } -(void)actiontwo { nslog(@"%@", self.varstring); }
Comments
Post a Comment