Chris
Chris

Reputation: 27394

iPhone - Animating "back" to parent controller

I want to implement a "Next" feature in my app. The button appears on a ViewController that is shown by clicking on a row in a UITableView. Ideally what I would like is for Next to do the following (in pseudo code)

  1. Go "back" to the UITableView
  2. Scroll to next available item in UITableView
  3. Select that item

I believe I can do 2,3 via selectRowAtIndexPath (please feel free to correct me if I am wrong) but I am unsure how to do Step 1. I will also have to target a method in the UITableView rather than the UIViewController that hosts the Button. How would I achieve this?

Update:

I can now target the rootViewController but now do I actually show it? (For reference here is my code, thanks to this question)

NSArray *viewControllers = self.navigationController.viewControllers;
UIViewController *rootViewController = [viewControllers objectAtIndex:viewControllers.count - 2];

UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:rootViewController action:@selector(nextButton:)];

Update 2

I now have the back animation working but it keeps the navigation bar intact, how do I also set this back to the correct (rootViewController) one?

[self.navigationController popToRootViewControllerAnimated:YES];

Upvotes: 1

Views: 211

Answers (1)

Alex
Alex

Reputation: 2468

If you have situation like this:

  1. RootViewController - tableView
  2. NextviewController - some view with Next button which shows after the you click on tableView

When you issue [self.navigationController popToRootViewControllerAnimated:YES]; from the 2. controller by pressing the Next button, it will bring RootViewController to the screen.

Now, on the RootViewController you have to "detect" back action (perhaps in ViewWillAppear) and continue with your steps 2. and 3.

Upvotes: 1

Related Questions