objective-c - IOS:处理一个对象

标签 objective-c ios xcode

我有这个代码:

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

相关文章:

objective-c - acosf() 返回 NaN

objective-c - 求解 objective-c 中的对数

objective-c - 如何用 Block 简化回调逻辑?

ios - 将 NSPrivateQueueConcurrencyType 子上下文与 NSPrivateQueueConcurrencyType 父上下文一起使用

ios - ios中如何将数据传递给父类?

objective-c - 在 NSTableView 中除所选行之外的所有行中放置渐变

iOS 构建错误 - 多个构建命令

xcode -- 基于构建配置的条件编译

ios - 当我使用 CFRelease 时应用程序崩溃

ios - Xcode Swift appleWatch 读取应用程序 UserDefaults?