gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:44:35 25 4
gpt4 key购买 nike

我有这个代码:

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com