记忆释放问题

时间:2010-12-08 13:56:05

标签: iphone objective-c

我创建了一个视图并将视图分配给viewcontroller

UIView *newView=[[UIView alloc] initWithFrame:CGRectMake(0,0,320,460)];

在viewDidLoad方法中我已将视图分配给viewcontroller

-(void)viewDidLoad{
    self.view=newView;
    //[view release]; In this case also the application crashing
}

-(void)dealloc{
   [newView release];//IN this case also the application crashing.
   [super dealloc];
}

崩溃日志就是这样。 alt text

如何发布newView?否则viewcontroller本身将负责释放newView。

1 个答案:

答案 0 :(得分:0)

在大多数情况下,您将执行以下操作:

- (void) loadView {
    // Don't call super here if you assign the view property
    UIView *newView = [[UIView alloc] initWithFrame: [UIScreen mainScreen].applicationFrame];
    self.view = newView;
    [newView release];
    // the view is now owned and retained by the UIViewController
}

Apple建议您在-loadView中分配视图,分配和发布视图,而不要在-dealloc中担心。这在您的视图可能在低内存条件下被清除的情况下非常重要。