3

I have a Navigation-Based app in which i added ABPeoplePickerNavigationController as a subview to my Navigation Contorller like this: I have saved the view before adding subview into navView.

ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;

navView = [[UIView alloc] init];
navView = [[self.view superview] superview];

[[[self.view superview] superview] addSubview:[peoplePicker view]];

It works fine, but when I'm done with the PeoplePicker and I want to get back to the previous view. I used this code but it doesn't work.

[[[self.view superview] superview] addSubview:navView];

I don't get it, I have saved the navView from the subview, now I can't bring it back?

Hadi Sharghi
  • 903
  • 16
  • 33

1 Answers1

1

You need to add the back button, like so:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];   

Ideally your NavigationController is the root of your application, in which case any other view is a subview of it so you never trully swich away from your navigation controller, you merely change the actual view that is shown.

Also the way in which you attempt to reference your first view is fairly messy/confusing. You should change it to something like:

[appDelegate.navigationController topViewController];    

or

[appDelegate.navigationController popToRootViewController];

I cant tell you for certain which to use as I am not sure of your exact view hierarchy, but I would highly recommend that you try to optimize that code.

I should probably note that the back button on the navigation controller and the the two lines below that do different things. The back button returns you one view back, while popToRoot and topView are both intended to bring you back to the very first viewController.

Edit: Changed the back button code to the code I actually use

Edit2: Just because I have a bit more space here I can try to understand your heirarchy better. It looks something like this?

Root View                     or                 Root View
-> Group View                                    -> Group View
ABPeoplePicker View                              -> ABPeople Picker View

Root View
-> Group View
-> -> ABPeoplePicker

Meaning that ABPeoplePicker is nested within Group, and that is nested within your root? I think that its not working because what you actually have is one of the two on the first line, when what you want is the second line. Because I don't actually know which one you have without seeing it, I cant tell you exactly what to change, but if you want it to be nested all the way like the heirarchy on the second line, you need to push your group view onto the root view controller(Which is your navigation controller), then you want to push People Picker onto the root view controller again. After this your back button and view switching should be working the way you want it to.

Karoly S
  • 3,180
  • 4
  • 35
  • 55
  • My NavigationController has the back button, but it goes to the first view (RootViewController). This is how I get to ABPeoplePicker view: RootViewController->GroupView->ABPeoplePickerNavigation but the back button doesn't go to GroupView, it goes to RootViewController. – Hadi Sharghi Aug 19 '11 at 21:51
  • I edited my answer to try to understand your hierarchy better, let me know if that is correct. I will try to get back to you as soon as I can. – Karoly S Aug 19 '11 at 21:58
  • Yes, the hierarchy is exactly like the one you draw. Except PeoplePicker didn't push with NavigationController. I tried this: `[self.navigationController popToViewController:self animated:YES];` but the GroupView is not shown, I think it's because I didn't push the PeoplePicker, so NavigationController can't pop another view back. I replaced it as a subview. How can I change it back to GroupView? – Hadi Sharghi Aug 19 '11 at 22:05
  • I've updated the answer with a bit more, I think the way you add your views to the root navigation controller is what is messing you up. – Karoly S Aug 22 '11 at 14:33