简体   繁体   中英

Pushing View Controller - viewDidAppear not called

I have this piece of code to push a view controller:

        // Setup the animation
[self.navigationController pushViewController:self.productView animated:YES];

self.productView.imageURL = [product imageURL];

// Set the title of the view to the product's name
self.productView.title = [product name];

// Set the label text of all the labels in the view
[self.productView.caloriesL setText:[product calories]];
[self.productView.fatL setText:[product fat]];
[self.productView.saturatesL setText:[product saturates]];
[self.productView.sugarL setText:[product sugar]];
[self.productView.fibreL setText:[product fibre]];
[self.productView.saltL setText:[product salt]];

But the delegate method viewDidAppear does not get called when the productView appears. I looked up the problem on google and theres a lot of different solutions, none of which I could apply to my problem.. I had a similar problem in a previous solution but I got around it by manually calling viewDidApear in the viewDidLoad method. Unfortunately in this case I can't do that as viewDidLoad is called only once (on the first push). Does anyone know how to fix this?

Thanks,

Jack Nutkins

EDIT:

Here is the viewDidAppear method in the productView (and selector):

- (void)viewDidAppear:(BOOL)animated{
//Start animating the activity indicator
[indicator startAnimating];
//Perform this method in background
[self performSelectorInBackground:@selector(loadImage) withObject:nil];

}

- (void) loadImage {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Load the animals image into a NSData boject and then assign it to the UIImageView
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
    UIImage *image = [[UIImage alloc] initWithData:imageData];
    self.imageView.image = image;

    //Stop animating the activity indicator
    [indicator stopAnimating];

    [pool drain]; //see comment below
}

First: You definitely don't want to be calling any of the standard viewWillLoad , viewDidLoad , viewWillAppear , etc. methods manually. Let the OS do it for you.

Second: Can you show us how your viewDidAppear method is implemented in your self.productView instance? (Just a hunch, you're not expecting this method to be called on your navigation controller, right?) I just want to make sure your method signature is exactly correct. If it's not (due to a mispelling, improper args, etc.) then it definitely won't be called.

Third: I would move your pushViewController: call to after the rest of the code you provided. You don't want the view to be pushed on the screen (so the user can see it) and then have a bunch of on-screen values immediately change. Set your ivars and title property first, then push the view controller. This eliminates any weird flickering.

I solved it, though it doesn't seem conventional, can't believe I didn't try it earlier :

I put this line :

[self.productView viewDidAppear:YES];

Underneath :

    // Setup the animation
[self.navigationController pushViewController:self.productView animated:YES];

I also moved the code to set the labels text to run before the above line. (As well as changing my code to send strings to the pushed controller rather that accessing its UI elements.)

Thanks for everyones help,

Jack

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