2

I have a method in a UIViewController to do things base on if it is currently appearing. I can put a BOOL flag to switch YES/NO when the viewDidAppear/viewDidDisappear is called but is there a better way/method to check?

Thanks.

evanwong
  • 5,054
  • 3
  • 31
  • 44
  • 1
    This might help: http://stackoverflow.com/questions/2777438/how-to-tell-if-uiviewcontrollers-view-is-visible – albertamg Jan 20 '12 at 16:53

1 Answers1

0

The viewDidAppear method is your best bet. I have seen some edge cases, though, where viewDidAppear/viewDidDisappear are not called depending on various factors.

Just because I enjoy the control and don't like any unknowns, I typically expose a method in my controllers that do all the setup when called and I would call this method when I know that my controller is getting called into action.

A rough example:

@interface MyController: UIViewController
- (void)reset;
@end

@implementation MyController
- (void)reset
{
    //Set some defaults, do some logging, etc
}
- (void)viewDidAppear
{
     [self reset];
}
- (void)viewDidLoad
{
     [self reset];
}
@end

Then...

@implementation SomeOtherController
- (void)someMethod
{
    [self.myController reset];

    //present self.myController using some logic

}
@end
Jeremy
  • 8,902
  • 2
  • 36
  • 44