开发者

Adding a subview to UINavigationController then back to the first subview?

开发者 https://www.devze.com 2023-03-29 22:55 出处:网络
I have a Navigation-Based app in which i added ABPeoplePickerNavigationController as a subview to my Navigation Contorller like this:

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] addSubv开发者_如何学运维iew:navView];

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号