IPhone: viewWillAppear only when returning from other views

viewWillAppear is called both when viewing and when returning to a view from other views.

I want to select (highlight) and fade the cell only when returning from other views.

Is there a delegate method for this?

I am using UINavigationViewController.

+4
source share
4 answers

If you are targeting iOS 5, you can use [self isBeingPresented] and [self isBeingDismissed] to determine if a view controller is added or removed from the nav controller.

I also suspect that you can improve the logic when you select / deselect a cell in your table view, so it doesn’t matter if the view controller is suitable or if it fits.

The usual way to do this is as follows: when someone selects a row in the table view in view controller A, he selects / highlights and you press the new view controller B. When view manager B deviates, you animate deselect the table row in viewDidAppear (so that the user can see its attenuation) in the controller A. You will not worry about whether only the controller of type A appeared or appeared again, because there will be only the selected cell of the table view in the corresponding case.

+3
source

If you are using iOS 5, you can use these new properties:

These four methods can be used in a controller callback view to determine whether it will be presented, rejected, added, or deleted as a child view controller. For example, a view controller can check if it disappears because it was rejected or popped up by requesting itself in its view of the WillDisappear: 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 use the UITableViewController , setting the -clearsSelectionOnViewWillAppear property to YES will do it for you. You need to do this manually if you are using a regular UIViewController with a UITableView view.

+11
source

viewWillAppear receives a call when a view appears

  • after watching DidLoad
  • after you miss or pull the view controller

You can change viewWillAppear to the following

 - (void) viewWillAppear:(BOOL)animated { static BOOL firstTime = YES; if (!firstTime) { //Do your alpha animation } firstTime = NO; } 
+4
source

In your UINav controller, you can create the "lastView" property and each of your view controllers (which are controlled by your UINav controller) sets this property to "viewWillAppear" ... in your target view ... the one you want to make selection and fade, you can check this property of the UINav Controller and see if it is NIL or not.

This is one way to do this. This will not work if you choose modal or the like.

0
source

All Articles