CosmicRabbitMediaInc
CosmicRabbitMediaInc

Reputation: 1175

viewDidAppear called repeatedly

I'm working on an app that displays a randomly chosen image that changes upon touch, and I'm presenting a modal view from viewDidAppear, which works as menu.

Apparently, whenever new image is loaded on main view, viewDidAppear is repeatedly called, so the menu modal view keeps popping up. The reason that I put presentModalView in viewDidAppear is because the modal view has to come up at the very beginning before main view, not by a touch-trigger from main view. I've tried presenting modal view elsewhere, but didn't really work.

any suggestion?

following is my viewDidAppear on main view, but there's not much information from this..

 -(void)viewDidAppear:(BOOL)animated
{
NSLog(@"viewDidAppear");
[self presentModalViewController:modalView animated:YES];
} 

my app constantly reloads the random image on every touch.

Upvotes: 1

Views: 2504

Answers (2)

mmccomb
mmccomb

Reputation: 13817

Add a property to your UIView subclass is initialised to false in your init method...

@interface RandomImageView : UIView {
    BOOL hasPresentedModalMenuView
}

@property(assign) BOOL hasPresentedModalMenuView;

Then in your viewDidAppear method check to see if the property is false. If so, present the view and set the property to TRUE.

- (void)viewDidAppear {
    if (!self.hasPresentedModalMenuView) {
        // Present view.....
        .....
        self.hasPresentedModalMenuView = TRUE;  
    }
}

That will prevent your modal menu view being displayed multiple times.

Upvotes: 1

Anomie
Anomie

Reputation: 94834

viewDidAppear is called when your app starts. Then you display a modal view, which makes your view disappear. When the modal view goes away, your view reappears so you get viewDidAppear called again. Lather, rinse, repeat.

It sounds like you want to set a flag on your object so you can ignore the subsequent viewDidAppear calls.

Upvotes: 2

Related Questions