何时释放选项卡栏应用程序中的对象?
这是我的问题的扩展此处
场景如下:
我有三个选项卡,其中有一个开关。当我触摸开关时,标签会更新(打开或关闭),并且灯泡的图像从一张 jpg 更改为另一张。所以我正在使用 UIImageView 来更改 UIImage。
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController {
UISwitch *switch1;
UILabel *status1;
}
@property (nonatomic,retain) IBOutlet UISwitch *switch1;
@property (nonatomic,retain) IBOutlet UILabel *status1;
- (IBAction) switch1Change;
@end
并且实现是
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize switch1;
@synthesize status1;
- (IBAction) switch1Change
{
if (switch1.on) {
status1.text = @"ON";
...
}
else {
status1.text = @"OFF";
...
}
}
我无法理解何时释放对象。我尝试在 - (void) dealloc {} 方法中给出 [switch1 release] 。但是当我切换标签时,应用程序崩溃了。如何做到这一点?
This is an extension to my question here
Here is the scenario:
I have three tabs in which a switch is there. When I touch the switch, a label gets updated (ON or OFF) and image of a bulb changes from one jpg to another. So I am using UIImageView in which I am changing the UIImage.
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController {
UISwitch *switch1;
UILabel *status1;
}
@property (nonatomic,retain) IBOutlet UISwitch *switch1;
@property (nonatomic,retain) IBOutlet UILabel *status1;
- (IBAction) switch1Change;
@end
and the implementation is
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize switch1;
@synthesize status1;
- (IBAction) switch1Change
{
if (switch1.on) {
status1.text = @"ON";
...
}
else {
status1.text = @"OFF";
...
}
}
I am not able to understand when to release the objects. I tried giving the [switch1 release] in - (void) dealloc {} method. But when I switch tabs, the app crashes. How to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dealloc 中的
[switch1 release]
似乎是正确的,您遇到的崩溃是什么?可能与此无关。如果您设置断点,您可能会发现选项卡没有被释放;一旦通过标签栏加载,它们就会保留在周围。此外,所有选项卡的视图控制器都会在启动时加载。
[switch1 release]
in dealloc seems correct, what is the crash you get? It could be unrelated to this.If you set a breakpoint you'll probably find the tabs do not get deallocated; once loaded by the tab bar it keeps them around. Also the view controllers for all tabs are loaded at start.