简体   繁体   中英

do I need to initialize the new view?

I admit I'm a newby to Objective-C but came pretty far, I guess. Now I'm having an issue and would need some help.

In my app I display images in portrait modes amongst other information and want to display them full screen in landscape mode. I learned that you'd do this defining a Landscape m, h and nib file and load this view when the device is moved to landscape orientation (working with an observer), right?

In portrait mode the images in the end depend on user interaction so the Landscape class needs to be told, which image to display. I created a putImage method in the Landscape class

-(void) putImage:(NSString *)theImage {

    pict.image = [UIImage imageNamed: theImage];

    NSLog(@"PICT  %@",pict.image);
    NSLog(@"IMAGE %@",theImage);
}

and call it from the portrait one and here comes the issue:

NSString *actualImage = [NSString stringWithFormat:@"Picture%i.jpg",selected];
pict.image = [UIImage imageNamed: actualImage];
[landscapeViewController putImage:actualImage];

that works perfectly every time except the very first one. So whatever I do I need to first make the App load a new image plus change to landscape orientation (being displayed no image) in any manner. Then after it always works displaying the correct images.

I added debug information and see that the correct image is assigned to pict in landscape class - however it seems it needs to be displayed before it really sets the image.

Any help you guys could give?

Not sure if this it it since your example code is pretty sparse. But when you load your first picture do you do that in init? If so, try to do it in viewWillAppear or viewDidAppear.

thank you for all the hints and tries to help. Now I found the solution myself ... here's what I did:

1) in the LandscapeViewController.h I defined an NSString:

@interface LandscapeViewController : UIViewController
{
    IBOutlet UIImageView *pict;
    NSString *actualImage;
}

2) I changed the putImage method to only assign the image name to my local NSSTRING variable:

-(void) putImage:(NSString *)theImage {

    actualImage = theImage;

    [actualImage retain];
}

3) loading the image I only do in viewWillAppear:

- (void)viewWillAppear:(BOOL)animated {

    pict.image = [UIImage imageNamed: actualImage];

    [super viewWillAppear:animated];
}

So the answer to my question is almost answered - I guess that this issue arose because I did set the image before I loaded the view first time so that it was not instanciated. I was able to call the method but had no Image Object to set ... does that sound logical to you guys?

Thank you all again!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM