1

I am developing a game with robovm & libgdx on ios, how to override UIViewController.viewWillAppear?

(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}
David Ansermot
  • 6,052
  • 8
  • 47
  • 82
Alan Yin
  • 41
  • 4

2 Answers2

3

You can override your method by providing a method with the same name as that of your parent class: that new method will replace the inherited definition. Make sure your method have the same return type and take the same number and type of parameters as the method you are overriding.

So just redefine the -(void)viewWillAppear:(BOOL)animated in your subclass.

Subclassing through usage of property is discouraged according to Apple Doc.

Community
  • 1
  • 1
tiguero
  • 11,477
  • 5
  • 43
  • 61
1

As usually you have two ways:

  • subclass your viewController and redefine -(void)viewWillAppear:(BOOL)animated

  • perform method swizzling in category for UIViewController (see for example Method Swizzle on iPhone device)

Community
  • 1
  • 1
malex
  • 9,874
  • 3
  • 56
  • 77