objective c - Using an allocated and initialised object outside of viewDidLoad? -


i hope question title adequate

just confused in piece of code have seen in online tutorial. there generic zhedog class declared properties, methods, etc. , class have created several instances - firstdog, seconddog, fourthdog, , on.

now, when created each instance did inside of viewdidload method of our main(one)view controller line:

zhedog *fourthdog = [[zhedog alloc] init]; 

and set of properties name, , on, here after line.

so did instance creation in view controller's viewdidload , far have not subclassed generic zhedog class, deriving 1 class file.

now, confused apparently cannot set property of instance in method (other viewdidload), can't like:

-(void) printhelloworld { fourthdog.name = "something new"; } 

it kind of makes sense can't explain why. have thought once instance allocated , initialised change properties wanted if necessary? same rules of scope apply viewdidload?

use properties, instance variables accessible everywhere within instance of class

@property zhedog *firstdog, *fourthdog; 

then instantiate them in viewdidload

self.firstdog = [[zhedog alloc] init]; self.fourthdog = [[zhedog alloc] init]; 

and change them in method

-(void) printhelloworld { self.firstdog.name = "woof";        self.fourthdog.name = "something new"; } 

Comments