I am updating an old app to the new adaptive size way of doing things and having difficulty getting a popover with a navigation controller to work.
My goal: I want to be able to open a popover from a button when the app is either compact and regular horizontal. The popover has a tableview and uses a navigation controller to push view controllers when the user touches a row on the table. I can get the popover to open correctly, but I can’t figure out who to make the pushes work.
Here’s the code that opens the popover:
OptionsController *vc = [[OptionsController alloc] initWithNibName:#"OptionsView" bundle:nil];
vc.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popover = [vc popoverPresentationController];
popover.delegate = self;
[self presentViewController:vc animated: YES completion: nil];
popover.permittedArrowDirections = UIPopoverArrowDirectionUp; // change as necessary
popover.sourceView = self.view;
CGRect popoverRect = [self.view convertRect:[sender frame] fromView:[sender superview]];
popover.sourceRect = popoverRect;
This code correctly opens a popover in either compact or regular size.
In the OptionsController’s didSelectRowAtIndexPath method, I have this(controllersArray is an array of UIViewControllers, each of which corresponds to a row in the table):
UIViewController *nextController = [self.controllersArray objectAtIndex: [indexPath row]];
[self.navigationController pushViewController:nextController animated:YES];
All this executes, but no push occurs, so the next view never appears.
I clearly am not understanding something about using the UIViewController’s navigationController, or how to install a navigationController to make this work. After three or four days of digging around to try to understand how to make this work, I'd appreciate any insights, or links to documentation about how to do this. Thanks in advance.
Crud - this has a very easy answer. Just took me thinking a different way and digging through Larcerax's comments. Here's how to make this work:
OptionsController *vc = [[OptionsController alloc] initWithStyle:UITableViewStylePlain];
vc.title = #"Options";
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController: vc];
nc.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popover = [nc popoverPresentationController];
popover.delegate = self;
[self presentViewController:nc animated: YES completion: nil];
popover.permittedArrowDirections = UIPopoverArrowDirectionUp; // change as necessary
popover.sourceView = self.view;
CGRect popoverRect = [self.view convertRect:[sender frame] fromView:[sender superview]];
popover.sourceRect = popoverRect;
The difference is that I create the UINavigationController in the usual manner...
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController: vc];
...set the navigationController's presentation style to popover, then get the popoverPresentationController from the navigationController - before I was doing those two methods on the UIViewController.
nc.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popover = [nc popoverPresentationController];
Finally, I present the navigationController:
nc.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popover = [nc popoverPresentationController];
This approach presents a popover, in compact and regular horizontal sizes, that contains navigation controller functionality: just what I wanted.
Thanks again to Larcerax whose answer wasn't what I needed, but made me re-think what I was doing in a different way. As usual, StackOverflow comes through.
Here's what you should try. I use about 10-40 navigation controllers per app for the apps I work on and I've had this same sort of issue, I've not used popovers, but I've subclassed the crap out of navigation controllers and view controllers to have encountered your same problem. The thing is that if you do this:
UIViewController * ff = [UIViewController new]
[self presnetViewController:ff ... blah blah blah
There is apparently NO navigation system attached to the modal view controller and therefore you can't navigate to anything else, you can only close the modal and move on. So, this is what I do to resolve this and it works everytime, well it works everytime for UIViewControllers, give it a shot
see the following, it's not for popovers, but the principle is the same:
NSHTermsOfServiceViewController * pvc = [NSHTermsOfServiceViewController new];
UIBarButtonItem * backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"exit-button"] style:UIBarButtonItemStylePlain target:self action:#selector(backerPressed)];
NSHNavigationController * ssf = [[NSHNavigationController alloc] initWithRootViewController:pvc];
[[pvc navigationItem] setLeftBarButtonItem:backBarButtonItem];
[[self navigationController] presentViewController:ssf animated:true completion:nil];
NSHTermsOfServiceViewController <== is a subclass of another subclass of a UIViewcontroller, it's basically a UIViewController on steroids, that's all
NSHNavigationController is a UINavigationController that is subclassed and pumped up on steroids for animations
The flow is this:
create a viewController
create a new UINavigationController
Set the view controller you created in step 1 as the root view controller of the navigation controller created in step 2
present the NAVIGATION CONTROLLER, not the UIViewController, you can present this navigationController from a view controller like so ..
v
[self presentViewController:ssf animated:true completion:nil];
or you can present it from the current view controller's navigation controller which is what I prefer, like so:
[[self navigationController] presentViewController:ssf animated:true completion:nil];
Your code, modified, the only problem is that I don't know if you can present a UIPopOverViewController by rooting it inside a navigation controller
OptionsController *vc = [[OptionsController alloc] initWithNibName:#"OptionsView" bundle:nil];
UIPopoverPresentationController *popover = [vc popoverPresentationController];
UINavigationController * stuff = [[NSHNavigationController alloc] initWithRootViewController:popover];
stuff.modalPresentationStyle = UIModalPresentationPopover;
stuff.delegate = self;
[self.navigationController presentViewController:stuff animated: YES completion: nil];
popover.permittedArrowDirections = UIPopoverArrowDirectionUp; // change as necessary
popover.sourceView = self.view;
CGRect popoverRect = [self.view convertRect:[sender frame] fromView:[sender superview]];
popover.sourceRect = popoverRect;
Yep, my bad, doesn't work for popover, I just tried it
So, with that said, is it absolutely necessary to use a popover? Why are you using this and now just a UIViewcontroller that you reconfigure to look like a popover and then you have what you need?
Here's this, I just tried it with the Ipad simulator, and it allowed a push, just as it should have.
NSHLoginViewController * pvc = [NSHLoginViewController new];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:pvc];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navController];
UIView * stuff = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 1000, 1000)];
[self.view addSubview:stuff];
[popover presentPopoverFromRect:[[self contentView] nameField].frame inView:stuff permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
this: [[self contentView] nameField].frame is just a uitextfield, nothing special, that's all, and the method above presented the login viewcontroller, when I put in my credentials, I pressed log in and it pusehed the next viewcontroller as it normally would, there's probalby something wrong with the touches being intercepted by your uitableview or whatever, perhaps not, but this method did work for me.
So I'm presenting a UIPopoverView like so:
if (self.BStatePopoverViewController == nil) {
RedStatePopoverViewController *settings =
[[RedStatePopoverViewController alloc]
initWithNibName:#"RedState"
bundle:[NSBundle mainBundle]];
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:settings];
popover.delegate = self;
self.BStatePopoverViewController = popover;
}
[BStatePopoverViewController setPopoverContentSize:CGSizeMake(320, 445)];
[self.BStatePopoverViewController presentPopoverFromRect:[sender bounds] inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Once the view is loaded in the popover, I have a button which I'd like to use to present a new UIViewController within the popover. I tried just presenting it as a modal view but this changes the parent view as opposed to the one in the popover:
PopupDischargeViewController * dischargeview = [[PopupDischargeViewController alloc] initWithNibName:#"PopupDischargeViewController" bundle:nil];
[self presentModalViewController:dischargeview animated:NO];
Any help as to how I do this would be much appreciated.
Thanks!
To change the view controller the popover should display once it's been initialized use UIPopoverController's method – setContentViewController:animated:
You could also add your RedStatePopoverViewController to a UINavigationController and use its methods – pushViewController:animated: and – popViewControllerAnimated: to navigate between view controllers
Hope this helps!
I am attempting to present a UIPopoverController similar to the bookmark selector in Safari with the title above and the ability to go back and forward. I found this tutorial: http://mobiforge.com/designing/story/using-popoverview-ipad-app-development and have followed it, yet my navigation bar is not appearing like it should be. Instead, it is appearing like it is recessed in the popover controller and not actually part of the popover controller. Below is the code I have so far. Any help would be greatly appreciated.
Code:
- (IBAction)addAPrePlan:(id)sender
{
if (![self.preplanPopoverController isPopoverVisible]) {
// Initiate the popover controller
UITableViewController *pp = [[UITableViewController alloc] init];
pp.navigationItem.title = #"Add a PrePlan";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:pp];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:nav];
popover.delegate = self;
self.preplanPopoverController = popover;
}
else {
// Dissmiss the popover
[self.preplanPopoverController dismissPopoverAnimated:YES];
}
// Present the popover controller
[self.preplanPopoverController presentPopoverFromBarButtonItem:self.addAPrePlanButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
I would like to create a subview which loads notifications similar to Facebook in its iOS app. Does anyone know how to build that?
Thanks!
Something like that:
http://www.techarp.com/article/Apple/Facebook_Notifications/facebook.jpg
I suggest you to use a UIPopoverController.
UIViewController *viewController = [[UIViewController alloc] initWithNibName:#"UIViewControllerNibName" bundle:nil];
viewController.delegate = self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navController];
navController.contentSizeForViewInPopover = CGSizeMake(400, 200);
viewController.contentSizeForViewInPopover = CGSizeMake(400, 200);
poopover.delegate = self;
CGRect rect = CGRectMake(0, 0, yourWidth, yourHeight);
[popover presentPopoverFromRect:locRect inView:parentView permittedArrowDirections:UIPopoverArrowDirectionLeft animated:TRUE];
viewController is the UIViewController inside the popover, and there you can add, for example, a UITableView to show notifications. UIPopoverController is responsible of showing the popup view. Of course in presentPopoverFromRect: method you have to pass the frame the popover anchors to, the view in which present the popover, the arrow directions allowed and the animate flag in order to show the popover in an animated manner. Finally, with delegation you can communicate to the class that is allocing the popover (self.delegate = self).
The iPad app pdf expert displays popovers which seem to consist only of the header. Which kind of popover is this? I can't find something about it in the Apple docs. But it seems to be a standard UI element. Or is it a custom view?
Its nothing like a custom view. Its just without UINavigationController.
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UIPopoverController * popover = [[UIPopoverController alloc] initWithContentViewController:viewController];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(644, 425);
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];
This will solve you problem. Check it out.!!