【问题标题】:Queries about ViewDidLoad关于 ViewDidLoad 的查询
【发布时间】:2011-01-04 12:06:27
【问题描述】:

我听说viewDidLoad()方法在代码执行过程中被调用了好几次。所以如果我alloc这个方法中的一些视图,它会在每次调用时创建一个新的pointer而不释放他们。

这是我的方法

-(void)viewDidLoad()
[b1 release];
[b2 release];
[b3 release];
[b4 release];
[b5 release];
[b6 release];
[b7 release];
[s1 release];
[s2 release];
[s3 release];
[s4 release];
[s5 release];
[s6 release];
[s7 release];

[bg release];
[bgview release];
[player release];

window = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 1026, 768)];
[window setImage:[UIImage imageNamed:@"backgrd99.png"]];
[self.view addSubview:window];

tree1 = [[UIImageView alloc]initWithFrame:CGRectMake(860,100,258,543)];
[tree1 setImage:[UIImage imageNamed:@"moving trees1.png"]];
[self.view addSubview:tree1];
//[tree1 release];

/*tree2 = [[UIImageView alloc]initWithFrame:CGRectMake(860,200,327,521)];
 [tree2 setImage:[UIImage imageNamed:@"moving trees00010001.png"]];
 [self.view addSubview:tree2];
 [tree2 release];*/

bg = [[UIImageView alloc]initWithFrame:CGRectMake(-1,-1, 1026, 768)];
[bg setImage:[UIImage imageNamed:@"room99.png"]];
[self.view addSubview:bg];

bgsofa =[[UIImageView alloc]initWithFrame:CGRectMake(-1,-1
                                                     , 1026, 768)];
[self.view addSubview:bgsofa];

bgview = [[UIView alloc]initWithFrame:CGRectMake(230, 358, 80, 68)];
//[bgview setBackgroundColor:[UIColor cyanColor]];
[self.view addSubview:bgview];

bgview1 = [[UIView alloc]initWithFrame:CGRectMake(500,100, 500,200)];
//[bg setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:bgview1];


sofapattelview =[[UIView alloc]initWithFrame:CGRectMake(530, 20, 440, 97)];
[sofapattelview setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:sofapattelview];

sofapattelimg =[[UIImageView alloc]initWithFrame:CGRectMake(0,0, 440, 97)];
[sofapattelimg setImage:[UIImage imageNamed:@"sofa-button1.png"]];
[sofapattelview addSubview:sofapattelimg];
// create the view that will execute our animation
/*colorbt =[UIButton buttonWithType:UIButtonTypeRoundedRect ];
colorbt.frame = CGRectMake(50, 50, 100, 50);
[colorbt setTitle:@"Paint" forState:UIControlStateNormal];
[colorbt addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:colorbt];*/

pattel = [[UIView alloc]initWithFrame:CGRectMake(58, 20, 440, 97)];
[pattel setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:pattel];

pattelimg =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 440, 97)];
[pattelimg setImage:[UIImage imageNamed:@"paint-button.png"]];
[pattel addSubview:pattelimg];

b1 =[[UIButton alloc] initWithFrame:CGRectMake(15, 33, 53, 51)];
[b1 addTarget:self action:@selector(B1) forControlEvents:UIControlEventTouchUpInside];
//[b1 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b1];

b2 =[[UIButton alloc] initWithFrame:CGRectMake(75, 33, 53, 51)];
[b2 addTarget:self action:@selector(B2) forControlEvents:UIControlEventTouchUpInside];
//[b2 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b2];

b3 =[[UIButton alloc] initWithFrame:CGRectMake(137, 33, 53, 51)];
[b3 addTarget:self action:@selector(B3) forControlEvents:UIControlEventTouchUpInside];
//[b3 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b3];

b4 =[[UIButton alloc] initWithFrame:CGRectMake(196, 33, 53, 51)];
[b4 addTarget:self action:@selector(B4) forControlEvents:UIControlEventTouchUpInside];
//[b4 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b4];

b5 =[[UIButton alloc] initWithFrame:CGRectMake(257, 33, 53, 51)];
[b5 addTarget:self action:@selector(B5) forControlEvents:UIControlEventTouchUpInside];
//[b5 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b5];

b6 =[[UIButton alloc] initWithFrame:CGRectMake(318, 33, 53, 51)];
[b6 addTarget:self action:@selector(B6) forControlEvents:UIControlEventTouchUpInside];
//[b6 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b6];

b7 =[[UIButton alloc] initWithFrame:CGRectMake(377, 33, 53, 51)];
[b7 addTarget:self action:@selector(B7) forControlEvents:UIControlEventTouchUpInside];
//[b7 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b7];


s1 =[[UIButton alloc] initWithFrame:CGRectMake(15, 33, 53, 51)];
[s1 addTarget:self action:@selector(S1) forControlEvents:UIControlEventTouchUpInside];
//[s1 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s1];

s2 =[[UIButton alloc] initWithFrame:CGRectMake(75, 33, 53, 51)];
[s2 addTarget:self action:@selector(S2) forControlEvents:UIControlEventTouchUpInside];
//[s2 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s2];

s3 =[[UIButton alloc] initWithFrame:CGRectMake(137, 33, 53, 51)];
[s3 addTarget:self action:@selector(S3) forControlEvents:UIControlEventTouchUpInside];
//[s3 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s3];

s4 =[[UIButton alloc] initWithFrame:CGRectMake(196, 33, 53, 51)];
[s4 addTarget:self action:@selector(S4) forControlEvents:UIControlEventTouchUpInside];
//[s4 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s4];

s5 =[[UIButton alloc] initWithFrame:CGRectMake(257, 33, 53, 51)];
[s5 addTarget:self action:@selector(S5) forControlEvents:UIControlEventTouchUpInside];
//[s5 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s5];

s6 =[[UIButton alloc] initWithFrame:CGRectMake(318, 33, 53, 51)];
[s6 addTarget:self action:@selector(S6) forControlEvents:UIControlEventTouchUpInside];
//[s6 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s6];

s7 =[[UIButton alloc] initWithFrame:CGRectMake(377, 33, 53, 51)];
[s7 addTarget:self action:@selector(S7) forControlEvents:UIControlEventTouchUpInside];
//[s7 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s7];

FileURL = [[NSURL alloc]initFileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Sunny" ofType:@"aac"]];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:FileURL error:nil];

//[changecolor setAlpha:0.0];
[campFireView setAlpha:0.1];
[bgview setAlpha:1];
[self.view addSubview:toolbar];
}

即使处理完每个指针 i alloc?? 我的应用程序仍然崩溃?

只是想知道这种方法是否正确??

【问题讨论】:

  • 你的语法对于objective-c方法是关闭的:)它应该是-(void) viewDidLoad;不是-(void) viewDidLoad();那将是 C 和不存在的 Obj-C 之间的混合。

标签: iphone objective-c xcode uiview


【解决方案1】:
如果系统必须卸载视图,

viewDidLoad 实际上可能会被多次调用(可能是出于内存目的)。例如,当你显示一个模态视图控制器时,下面的视图可能会被卸载,当模态视图被关闭时,viewDidLoad 会被调用。

因此,如果您在viewDidLoad 中分配对象,您可能会泄漏它们。这就是为什么你应该在viewDidUnload 中发布它们。在你的情况下:

- (void)viewDidUnLoad {
    [b1 release], b1 = nil;
    [b2 release], b2 = nil;
    [b3 release], b3 = nil;
    [b4 release], b4 = nil;
    [b5 release], b5 = nil;
    [b6 release], b6 = nil;
    [b7 release], b7 = nil;
    [s1 release], s1 = nil;
    [s2 release], s2 = nil;
    [s3 release], s3 = nil;
    [s4 release], s4 = nil;
    [s5 release], s5 = nil;
    [s6 release], s6 = nil;
    [s7 release], s7 = nil;

    [bg release], bg = nil;
    [bgview release], bgview = nil;
    [player release], player = nil;

    [super viewDidUnload];
}

【讨论】:

  • 还有其他方法可以使用这种技术吗???自动释放不会在需要时释放内存..即导致应用程序崩溃.. :(
  • 我将首先使用“构建和分析”选项和仪器跟踪内存泄漏...请务必检查Memory Management Programming Guide。如果您的应用程序因内存不足而崩溃,则可能会在此之前收到内存警告。您的视图控制器和应用程序委托收到“didReceiveMemoryWarning”。在这种方法中,您应该释放内存对象,以便以后轻松地重新创建。
【解决方案2】:

ViewDidLoad 仅在 viewController 加载时调用一次。viewWillAppear 在视图刷新时调用多次。

【讨论】:

  • -viewDidLoad 每次加载视图时都会调用一次,但单个 UIViewController 可能会多次加载和卸载它的视图。
【解决方案3】:

尝试探索来自 Apple 的 Memory Management Programming Guide

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多