我有这个代码:

-(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
}


如您在示例中看到的,我使用FirstViewController类的对象“ firstViewController”,但是是否要在方法“ do”中使用相同的对象?我能怎么做

最佳答案

在标题中有...

FirstViewController *firstViewController;


然后将您的方法替换为...

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

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


并添加..

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

关于objective-c - iOS:处理对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6387216/

10-13 04:03