Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: viewWillAppear only when coming back from other views

Tags:

ios

iphone

viewWillAppear is called both when going to the view and when coming back to the view from other views.

I want to select(highlight) and fade-out a cell only when coming back from other views.

Is there a delegate method to do this?

I'm using UINavigationViewController.

like image 785
js_ Avatar asked Jun 08 '12 13:06

js_


1 Answers

If you're on iOS 5, you can use these new properties:

These four methods can be used in a view controller's appearance callbacks to determine if it is being presented, dismissed, or added or removed as a child view controller. For example, a view controller can check if it is disappearing because it was dismissed or popped by asking itself in its viewWillDisappear: method by checking the expression ([self isDismissing] || [self isMovingFromParentViewController]).

- (BOOL)isBeingPresented __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- (BOOL)isBeingDismissed __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);

- (BOOL)isMovingToParentViewController __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- (BOOL)isMovingFromParentViewController __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);

In your code:

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

    if (!(self.isMovingToParentViewController || self.isBeingPresented))
    {
        // animate
    }
}

EDIT:

If you're using a UITableViewController, setting the property -clearsSelectionOnViewWillAppear to YES will do this for you. You only have to do it manually if you're using a regular UIViewController with a UITableView subview.

like image 163
Ryder Mackay Avatar answered Nov 15 '22 11:11

Ryder Mackay