2011-04-26 53 views
0

上添加子视图,tempview2根本没有显示出来。只有tempview显示出来。无法在UIViewController中的viewdidload

   
-(id) init:(NSData*) imageData 
{ 
    if ((self = [super init])) {  

    tempimage= [[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pic1" ofType:@"png"]] autorelease]; 
    tempview=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease]; 
     [tempview setImage:tempimage]; 
     [self.view addsubview tempview]; 
} 
return self; 

} 

    -(void) viewdidload{ 
     tempimage2= [[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pic2" ofType:@"png"]] autorelease]; 
    tempview2=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease]; 
     [tempview2 setImage:tempimage2]; 
     [self.view addsubview tempview2]; 

} 

,如果我这样做,那么一切都OK,

 
-(id) init:(NSData*) imageData 
{ 
    if ((self = [super init])) {  

    tempimage= [[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pic1" ofType:@"png"]] autorelease]; 
    tempview=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease]; 
     [tempview setImage:tempimage]; 
     [self.view addsubview tempview]; 
     tempimage2= [[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pic2" ofType:@"png"]] autorelease]; 
    tempview2=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease]; 
     [tempview2 setImage:tempimage2]; 
     [self.view addsubview tempview2]; 
} 
return self; 

} 

回答

6

这不是直接回答你的问题,但视图控制器的初始化和视图装载的一般的好做法:

  1. 在init方法中,您只应初始化与视图不相关的ivars或其他类数据。否则会打破延迟加载的概念。在你的视图控制器的init方法中,视图还不存在。
  2. 在viewDidLoad方法中,您应该执行视图设置。在这个阶段,视图实际上被加载了,现在是正确设置它的时候了。

我会鼓励你将视图设置调用移动到viewDidLoad中,看看问题是否存在。

祝你好运。

+0

好的,我把所有东西都移到了viewdidload。发生什么 - (id)init:(NSData *)imageData我从外部获取图像数据并显示。我首先将图像数据保存到磁盘,然后将其带到viewdidload上查看。好的一点是不要在初始化例程中设置视图。 – lilzz 2011-04-26 20:44:42

+0

如@siddharth Iyer所述,调用[super viewDidLoad]。这很可能是你的问题的原因。并感谢您的投票。 – MiKL 2011-04-27 06:32:05

0

我同意MiKL。

但无论如何回到你的问题你忘了打电话给[超级viewDidLoad]?这可能是问题吗?

另外,作为一种最佳实践,在将子视图添加到其他子视图(将子视图添加到另一个视图增加它的保留数)后释放视图。

相关问题