Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call the ViewDidAppear only one time?

When the user start the app I want it show the stockholm.xib and It does here:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    NSUserDefaults *startPage =[NSUserDefaults standardUserDefaults];
    NSString *page =[startPage stringForKey:@"page"];
    NSLog(page);

    if(page==nil)
    {
        //Do nothing

    }
    else if ([page isEqualToString:@"Default"])
    {
        //Do nothing
    }

    else if ([page isEqualToString:@"Stockholm"])
    {
        NSLog(@"going to Stockholm");
        Stockholm *Start =[[Stockholm alloc]initWithNibName:nil bundle:nil];
        [self presentModalViewController:Start animated:YES];


    }
    else {
        NSLog(@"HAHA");

}

but when user closes the stockholm.xib using:

[self dismissModalViewControllerAnimated:YES];

after the animation is done, the app crashes. and the reason is, I guess, viewDidAppear calls twice and therefore it is trying to open the recently closed xib file.

now, how Can I call the view did appear only once? so that when the user comes back from Stockholm the viewDidAppear wont be called? any other solution?

thanx :)

like image 906
hafhadg3 Avatar asked Feb 14 '10 02:02

hafhadg3


People also ask

Can viewDidAppear be called multiple times?

viewDidAppear callback can be called multiple times in a single presentation.

How many times viewDidAppear is called?

If both these methods are called, what's the difference between the two? viewDidLoad() is only called once, when the view is loaded from a . storyboard file. viewWillAppear(_:) is called every time the view appears. In this simple app, that means it is only called once.

What is called after viewDidAppear?

viewDidAppear is called once you see the loaded view on screen. It is called after view appeared. ViewDidAppear is called everytime when you see the view after it is loaded.

Which is called first viewDidLoad or viewDidAppear?

The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.


2 Answers

You can try moving all that functionality in the viewDidLoad: method instead of the viewDidAppear. That one only fires once. Unless there is a reason for you not to...?

EDIT: more code to show what i mean in the comment

in the .h file:

BOOL firstTime;

in the .m file:

-(void)viewDidLoad {
   NSLog(@"viewDidLoad actually fired");
   //...
   firstTime = YES;
}
-(void)viewDidAppear {
   //...
   if(firstTime){
      //show it
      firstTime = NO;
   }
}
like image 73
Dimitris Avatar answered Sep 19 '22 05:09

Dimitris


You can use GCD, too, if your class has a field such as:

@interface MyClass {
    dispatch_once_t once;
}

@end

@interface MyClass {
    - (void)viewDidAppear:(BOOL)animated {
        dispatch_once(&once, ^{
            // do business
        });
    }
@end
like image 43
ZaBlanc Avatar answered Sep 21 '22 05:09

ZaBlanc