Using data in classes when button pressed objective C/iOS -


new ios , can't figure out simple thing. (been googling ages)

so i've got class i've initiated in viewcontroller.m viewdidload,

student *person = [[student alloc] init];  person.firstname = @"test"; 

i've created button, , on button pressed method i've tried nslog whatever's in firstname variable keep getting null result. add initialisation method , works fine.

is there anyway can call data that's been added viewdidload?

thanks

edit:

@implementation viewcontroller  - (void) viewdidload { [super viewdidload];      student *person = [[student alloc] init];      person.firstname = @"test";  }  - (ibaction)buttonpressed:(id)sender {  nslog(@"%@", person.firstname); } @end 

you have declared person locally viewdidload buttonpressed: method know nothing of it. change below , read comments explanation.

myviewcontroller.h

@interface myviewcontroller : uiviewcontroller  // no need create ivar done automatically // declaring property here make public if don't want public  // code above @implementation @property (nonatomic, strong) student *person;   - (ibaction)buttonpressed:(id)sender;  @end 

myviewcontroller.m

@interface myviewcontroller() // doing here make private class only. // @property (nonatomic, strong) student *person;  @end  @implementaiton myviewcontroller  // no need @synthesize done automatically // because @synthesize called automatically can use _person or self.person  - (void)viewdidload {     // initialization of person instance     _person = [[student alloc] init];     // setting firstname of person instance     _person.firstname = @"test"; }  - (ibaction)buttonpressed:(id)sender {     // nslog firstname     nslog(@"student's first name : %@", _person.firstname); }  @end 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -