cyclingIsBetter
cyclingIsBetter

Reputation: 17581

IOS: handle an object

I have this code:

-(IBAction) doSomething{ 
FirstViewController *firstViewController = [[[FirstViewController alloc]init]autorelease];
[firstViewController.label1 setAlpha:1.00];
[firstViewController.label2 setAlpha:1.00];
}

-(void) do{
//use firtsViewController in this method
}

As you can see in the example I use an object "firstViewController" of FirstViewController class, but if I want to use the same object in method "do"? How can I do

Upvotes: 0

Views: 78

Answers (2)

Simon Lee
Simon Lee

Reputation: 22334

In the header have...

FirstViewController *firstViewController;

Then replace your method with...

-(IBAction) doSomething{ 
if(firstViewController == nil) {
  firstViewController = [[FirstViewController alloc]init];
}

[firstViewController.label1 setAlpha:1.00];
[firstViewController.label2 setAlpha:1.00];
}

and add..

- (void)dealloc {
  [firstViewController release];
  [super dealloc];
}

Upvotes: 5

Ben Heymink
Ben Heymink

Reputation: 1700

Your 'firstViewController' is local only to your doSomething method, outside of this method it does not exist! If you want to use it across methods, consider implementing it as a member variable of the class containing the methods you use it from?

Upvotes: 0

Related Questions