上添加子视图,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; }
好的,我把所有东西都移到了viewdidload。发生什么 - (id)init:(NSData *)imageData我从外部获取图像数据并显示。我首先将图像数据保存到磁盘,然后将其带到viewdidload上查看。好的一点是不要在初始化例程中设置视图。 – lilzz 2011-04-26 20:44:42
如@siddharth Iyer所述,调用[super viewDidLoad]。这很可能是你的问题的原因。并感谢您的投票。 – MiKL 2011-04-27 06:32:05