2

I want to show a full page image Ad every time a UIViewController is shown. I think I have to call the method inside a viewDidAppear or ViewWillAppear, but they are being called once.

- (void) viewDidAppear:(BOOL)animated{
    [self showAds];
}

- (void) showAds{
    //Do Something
}

What should I do to call a method every time a uiviewcontroller is shown( even if its already created)?

iPatel
  • 46,010
  • 16
  • 115
  • 137
marceloquinta
  • 713
  • 12
  • 20
  • 2
    call method in ViewWillAppear it will call every time when you enter in particular view. – Pratik Mar 28 '13 at 03:58
  • be sure that after you use the view controller , every time you dismiss it , you can check it by viewdiddisappear . If the view is being presented and then not being dismissed , the view did appear will not be called . hope this is of some help. – Singh Mar 28 '13 at 07:00
  • when you say 'shown' what do you mean exactly? Come back from the background? Or pushed from a navigation controller, presented modally, something like that? – Carl Veazey Mar 28 '13 at 09:08
  • when I say shown is come to foreground. Like: unlocking the ipad and opening (again) the app, the adds must be shown. – marceloquinta Mar 28 '13 at 17:41

3 Answers3

5

ViewWillAppear will be called every time a UIViewController is shown,but won't be called when the app is back to foreground.

DD_
  • 7,230
  • 11
  • 38
  • 59
Marco Lee
  • 121
  • 5
4

you can use Notification to achieve your goal by following code, This scenario is specially when your app is in background and user press HOME button to active it.

Register for Notifcation when your application enterForground in viewDidLoad only.

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleEnteredBackground)
                                             name: UIApplicationDidBecomeActiveNotification
                                           object: nil];

write a method to invoke when application enterForground

-(void)handleEnteredBackground
{
    NSLog(@"%s",__FUNCTION__);
    // Your stuff here
}

Dont forget to Remove Observer in viewDidUnload method

[[NSNotificationCenter defaultCenter] removeObserver:self];

Post New Notification everytime your application enterForground

- (void)applicationWillEnterForeground:(UIApplication *)application
{

    [[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidBecomeActiveNotification object:nil];
}
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
1

ViewWillAppear should be called every time. Use:

- (void) viewWillAppear:(BOOL)animated{
    [self showAds];
}
Tim L
  • 170
  • 6
  • http://stackoverflow.com/questions/1579550/uiviewcontroller-viewdidload-vs-viewwillappear-what-is-the-proper-division-of – Tim L Mar 28 '13 at 03:50
  • 3
    viewWillAppear is called when the uiviewcontroller is loaded. Supposing: the user hits the home button after the view load. when the user enter in the app again, viewWillAppear will no be called. Test it. – marceloquinta Mar 28 '13 at 04:06
  • 3
    @marceloquinta viewWillAppear will be called each time the UIViewController's view is placed into the View Hierarchy. Your example of hitting the home button puts the app "to sleep" and has nothing do to with anything. For the home button example, talk a look at NSApplicationDelegate docs. – John Mar 28 '13 at 13:44