您是否必须在dealloc中发布IBOulets? [英] Do you have to release IBOulets in dealloc?

查看:76
本文介绍了您是否必须在dealloc中发布IBOulets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否必须在dealloc中发布IBOulets?我不确定这个,因为我没有做alloc,通常你只发布你所谓的alloc。有人知道吗?

解决方案

你的IBOutlets可能是 @properties 。如果它们是,并且你有 retain 作为属性,那么你需要在 -dealloc

换句话说:

  @interface MyViewController:UIViewController {
IBOutlet UITableView * myTable;
}

@property(非原子,保留)IBOutlet UITableView * myTable;

@end

您必须 [你的dealloc中的myTable发布];



如果你在Xcode中创建一个新的基于导航的应用程序,并查看appdelegate.h:

  @interface Untitled1AppDelegate:NSObject< UIApplicationDelegate> {

UIWindow *窗口;
UINavigationController * navigationController;
}

@property(非原子,保留)IBOutlet UIWindow *窗口;
@property(非原子,保留)IBOutlet UINavigationController * navigationController;

@end

和appdelegate.m的dealloc:

   - (void)dealloc {
[navigationController release];
[窗口发布];
[super dealloc];
}

这里看到的关键是这样的行:

  @property(非原子,保留)IBOutlet UIWindow *窗口; 

如果有保留,这意味着该属性被您的代码拥有并且你必须释放它。



当然,还有其他方法,例如不将IBOutlets声明为属性,或者将它们声明为属性而不保留。我发现在大多数情况下我更喜欢将它们保留为属性,然后我必须明确地释放它们。例如,当您从一个视图控制器切换到另一个视图控制器时。当一个视图控制器被关闭时,其视图将被删除并被释放。如果我没有保留它们,该视图上的任何IBOutlet UILabel也将被释放。这意味着当我回到旧视图时,我必须通过并将标签和控件重置为其最后的值,如果我只保留IBOutlet,我可以轻松保存它们。


Do you have to release IBOulets in dealloc? I'm not sure on this one because I didn't do the alloc and typically you only release for something you called alloc on. Anyone know?

解决方案

Your IBOutlets are probably @properties. If they are, and you have retain as an attribute, then you do need to release in -dealloc

In other words:

@interface MyViewController : UIViewController {
    IBOutlet UITableView *myTable;
}

@property (nonatomic, retain) IBOutlet UITableView *myTable;

@end

You will have to [myTable release]; in your dealloc.

If you make a new Navigation Based App in Xcode, and look in the appdelegate.h:

@interface Untitled1AppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UINavigationController *navigationController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end

and the dealloc for appdelegate.m:

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

The key thing to see here are lines like these:

@property (nonatomic, retain) IBOutlet UIWindow *window;

If there is a retain in there, that means that the property is being "owned" by your code and you have to release it.

Of course, there are other ways, such as not declaring the IBOutlets as properties, or declaring them as properties without retaining. I find that in most cases I prefer to have them be retained properties, which I then have to explicitly release. An example of this is when you flip from one view controller to another. As one view controller is dismissed, its views are removed and they are released. Any IBOutlet UILabels on that view would get released too if I don't have them retained. That means that when I flip back to the old view, I have to go through and reset my labels and controls to their last values, when I could have easily kept them saved if I just retain the IBOutlet.

这篇关于您是否必须在dealloc中发布IBOulets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆