0
votes

inside the UIViewController, the tempview2 is not showing up at all. only tempview is showing up.

        
-(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];

}

if I do this then everything is 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;

}

2

2 Answers

6
votes

This is not a direct answer to your question but a good practice of viewcontroller initialization and view loading in general:

  1. In the init method you should only initialize ivars or other class data which does not pertain to the view. Doing otherwise breaks the notion of lazy loading. During the init method of you view controller the view does not exist yet.
  2. In the viewDidLoad method you should perform your view setup. At this stage the view is actually loaded and it is the time to set it up properly.

I would encourage you to therefore move your view setup calls into the viewDidLoad and see if the problem persists or not.

Good luck.

0
votes

I agree with MiKL.

But anyway getting back to your question did you forget to call [super viewDidLoad]? Maybe that's the issue here?

Also, as a best practice, release views after adding them as subviews to something else (adding a view as a subview to another view increases it's retain count).