1

I am writing an iOS app that has a 3-4 level of drill down. In the 2nd, 3rd... and so on views, I want to add a home button on the nab bar, which once pressed takes you back to the root view controller, i.e. the first screen.

I have added a button using storyboard and simply want to implement the - (void) homeButtonpressed:(id)sender function so that my first view is displayed.

Also, if this is some kind of a built in functionality for this (like there is for back button), then that would be great, as I will need to write this piece of code in all my child views.

Can somebody help me with this?

Thanks!

user1534235
  • 161
  • 3
  • 16

2 Answers2

1

You can pop to rootview by using the following code

[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex: 0] animated: YES];

or

[self.navigationController popToRootViewControllerAnimated:YES];
Vinodh
  • 5,262
  • 4
  • 38
  • 68
  • Another related question: In my app, I have a table view with several options, like: name, country etc. For these table view cells, I also have a toolbar at the bottom of the view. On click of the tableview cell, the next view is triggered with the help of segues. But how do I trigger this view with the toolbar button? I need to pass some data as well to the view controller. – user1534235 Nov 02 '12 at 07:50
  • the following link will help you http://agilewarrior.wordpress.com/2012/01/25/how-segue-in-ios-and-pass-data-from-one-viewcontroller-to-another/ – Vinodh Nov 02 '12 at 09:56
  • I do know how to use segues, but using segues will mean a lot of duplicate code. I'll add segues in my parent tableview file, plus in all the toolbar files. Is there no other option here? – user1534235 Nov 02 '12 at 10:06
  • write one method which accepts input as seague as input use that method where you want to use – Vinodh Nov 02 '12 at 10:53
0

Make an array of your view controllers Pops all the view controllers on the stack except the root view controller and updates the display.

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated

[self.navController popToRootViewControllerAnimated:(BOOL)animated];
Vinodh
  • 5,262
  • 4
  • 38
  • 68
RKY
  • 266
  • 1
  • 7