1

I am making an application which has a main TableView in which every cell has details. when cell is clicked DetailView of that cell shows details. DetailView also has next and prev button from which we can go to next cell information without going back to main TableView. I want Back button in DetailView to work like that whenever i ll click it, application should show main TableView. Is there any way to do that?

I think i didnt explain my problem well. I have items in main TableView cells. and every cell creates a detail view when i click on that. there are next and previous buttons in every detailview so we can check next item details without going back to main table view. there is a back button on every detail view. for example if u clicked 3rd cell to see details. and then u went to 4th item's detail by using next button. now if u ll click back button it ll go back to 3rd view detail not the main table view. i wanna go back to main table view whenever i ll click back button. thats the problem.

Piscean
  • 3,069
  • 12
  • 47
  • 96

3 Answers3

2

s u can do this by

       [self.navigationController popViewController:YES];

through this without using back button. when u click on cell detailview, it goes back to the view

Splendid
  • 1,061
  • 6
  • 16
  • I think i didnt explain my problem well. I have items in main TableView cells. and every cell creates a detail view when i click on that. there are next and previous buttons in every detailview so we can check next item details without going back to main table view. there is a back button on every detail view. for example if u clicked 3rd cell to see details. and then u went to 4th item's detail by using next button. now if u ll click back button it ll go back to 3rd view detail not the main table view. i wanna go back to main table view whenever i ll click back button. thats the problem. – Piscean Feb 10 '11 at 14:13
  • then u could use popToRootViewController, – Splendid Feb 10 '11 at 14:18
  • or else u can set target for Button – Splendid Feb 10 '11 at 14:23
2

Main view is called Root, use this

[self.navigationController popToRootViewControllerAnimated:YES];

k-thorat
  • 4,873
  • 1
  • 27
  • 36
  • i am creating button with this code: UIBarButtonItem *tempButtonItem = [[[UIBarButtonItem alloc] init] autorelease]; tempButtonItem.title = @"Back"; .How can i tell that whenever its clicked run this statement: [self.navigationController popToRootViewControllerAnimated:YES]; – Piscean Feb 10 '11 at 14:16
  • [tempButtonItem addTarget:self action:@selector(goBackToMainView) forControlEvents:UIControlEventTouchUpInside]; and write a function named goBackToMainView ... in side this write the [self.navigationController popToRootViewControllerAnimated:YES]; – k-thorat Feb 10 '11 at 14:33
  • -(void)goBackToMainView { [self.navigationController popToRootViewControllerAnimated:YES]; } i made this function in DetailViewController.m and also defined in DetailViewController.h. and then [tempButtonItem addTarget:self action:@selector(goBackToMainView) forControlEvents:UIControlEventTouchUpInside]; where i m creating that back button. but i got warning: 'UIBarButtonItem' may not respond to '-addTarget:action:forControlEvents:' – Piscean Feb 10 '11 at 14:47
  • Add sender to the goBackToMainView method.. does it work with or warning or no.? – k-thorat Feb 10 '11 at 15:51
  • -(void)goBackToMainWindow:(id)sender . still not working. same warning – Piscean Feb 10 '11 at 15:57
  • code looks like this now: UIBarButtonItem *tempButtonItem = [[[UIBarButtonItem alloc] init] autorelease]; tempButtonItem.title = @"Tillbaka"; [tempButtonItem addTarget:self action:@selector(goBackToMainView:) forControlEvents:UIControlEventTouchUpInside]; and funciton in DetailViewController.m looks like this: -(void)goBackToMainView:(id)sender { NSLog(@"in go BAck"); [self.navigationController popToRootViewControllerAnimated:YES]; } – Piscean Feb 10 '11 at 16:02
1

Have you tried to push the detailview like this:

YourDetailView * detailView = [[YourDetailView alloc] init];

[self.navigationController pushViewController:detailView animated:YES];

[detailView release];

Elias
  • 504
  • 3
  • 9
  • I think i didnt explain my problem well. I have items in main TableView cells. and every cell creates a detail view when i click on that. there are next and previous buttons in every detailview so we can check next item details without going back to main table view. there is a back button on every detail view. for example if u clicked 3rd cell to see details. and then u went to 4th item's detail by using next button. now if u ll click back button it ll go back to 3rd view detail not the main table view. i wanna go back to main table view whenever i ll click back button. thats the problem. – Piscean Feb 10 '11 at 14:13
  • Hmm. Are you pushing a new viewcontroller each time you click next/previous button? What happens when you click the back button on the first detailview which appears after the TableView? – Elias Feb 10 '11 at 14:19
  • yup i m pushing a new view controller each time i click next/previous. and when i click the back button on the first detailview it goes on rootview. – Piscean Feb 10 '11 at 14:33
  • So now everything makes sense. You should add subviews to your detailviewcontroller, not viewcontrollers. Each time you press the next/previuos button, remove the current subview and then add a new subview. You're always in one viewcontroller which is detailviewcontroller. – Elias Feb 10 '11 at 14:37
  • sounds good. any nice link for creating and adding subviews. i am new and just understood little how viewcontrollers switch. – Piscean Feb 10 '11 at 14:51
  • This tutorial shows how to add/remove subviews: http://ykyuen.wordpress.com/2010/04/05/iphone-add-and-remove-subviews/ – Elias Feb 10 '11 at 15:02