简体   繁体   English

iPhone开发人员-以编程方式创建的视图中的viewDidLoad和viewDidUnload?

[英]iPhone dev - viewDidLoad and viewDidUnload in programmatically created view?

I read somewhere that in a programmatically created view in a UIViewController, not using Interface Builder, -viewDidLoad and -viewDidUnload should not be used. 我在某个地方读到某个内容,该内容是在UIViewController中以编程方式创建的视图中而不是使用Interface Builder生成的,不应使用-viewDidLoad-viewDidUnload Is this right? 这是正确的吗? Why? 为什么? Where would I release subviews that I have retaining properties of? 我将在哪里发布具有保留属性的子视图? Or should I just not use properties for them? 还是我不应该为它们使用属性?

EDIT : Read my comments on Rob Napier's answer. 编辑 :阅读我对罗布·纳皮尔的回答的评论。

Create your subviews in -viewDidLoad . -viewDidLoad创建子视图。 If you need ivars for them then only assign their values. 如果您需要ivars,则仅分配其值。 The reference is hold by adding the views as subviews to you main view. 通过将视图作为子视图添加到主视图来保持引用。

Then when your view is unloaded you should set your ivars to nil, because the object have been released since your view was removed and released. 然后,在卸载视图时,应将ivars设置为nil,因为自从删除和释放视图以来该对象已被释放。

So in your header 所以在您的标题中

@interface MyViewController : UIViewController {
  IBOutlet UIView *someSubview; // assigned
}
@property (nonatomic, assign) IBOutlet UIView someSubview;
@end

And in your implementation 在您的实施中

@implementation MyViewController
//... some important stuff

- (void)viewDidLoad;
{
  [super viewDidLoad];
  someSubview = [[UIView alloc] initWithFrame:self.view.bounds];
  [self.view addSubview:someSubview]; // retains someSubview
  [someSubview release];   // we don't hold it
}

- (void)viewDidUnload;
{
  [super viewDidUnload];
  someSubview = nil;  // set the pointer to nil because someSubview has been released
}

//... more important stuff

@end

If you wish you can also not release someSubview in -viewDidLoad , but then you have to release it in -viewDidUnload AND -dealloc since (if I remember right) -viewDidUnload isn't called before -dealloc . 如果您希望也不能在-viewDidLoad释放someSubview ,但是必须在-viewDidUnload AND -dealloc释放它,因为(如果我没记错的话) -viewDidUnload-dealloc之前没有被调用。 But this isn't necessary if you don't retain someSubview . 但是,如果您不保留someSubview则不必这样做。

the strange thing here is that an UIViewController not loaded from a NIB file is not notified about its view unloading (and so its viewDidUnload method is not called) unless you offer a base implementation of the loadView method, such as: 奇怪的是,除非您提供loadView方法的基本实现,否则未从NIB文件未加载的UIViewController不会收到有关其视图卸载的通知(因此不会调用其viewDidUnload方法)。

- (void)loadView {
   self.view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
   [self.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
}

- (void)viewDidLoad {
   [super viewDidLoad];
   // create views...
}


- (void)viewDidUnload {
   // destroy views...
   [super viewDidUnload];
}

this only happens to base UIViewController, an UITableViewController for example don't need to be fixed with this workaroud. 这仅发生在基本的UIViewController上,例如,不需要使用此workaroud修复UITableViewController。

So Robs is right. 所以罗伯斯是对的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM