How to correctly instantiate a PopoverController - iphone

I have a SplitviewController with multiple possible Detailviews (Webviews, Tableviews, regular UIViews).
As an example, I have a NavigationController on top, then navigate through some tables.
Finally I show some Content, lets say a UIWebview. I rotate the iPad to portrait, and in the toolbar I add a Button from which the popOverController is displayed.
On Buttonclick I say:
if (!popoverController) {
if (self.view.window != nil) {
popoverController = [[UIPopoverController alloc] initWithContentViewController: [appDelegateiPad naviPad]];
popoverController.delegate = self;
}
}
Here, I instantiate a PopOverController and the Content is the left part of the splitview, from the point I left off. All is nice.
But when I rotate, I get this warning:
CoreAnimation: ignoring exception: Popovers cannot be presented from a view which does not have a window.
And on the screen the popOverController reappears with empty content (black translucent I would say) but I don't know why, since I dismissed it and there cant be another instance since I only create one on buttonclick.
This has been driving me crazy for days.
ANY(!) help is appreciated!
-(void) showPopOver:(id) sender {
NSLog(#"showing popover?");
if (!popoverController) {
if (self.view.window != nil) {
popoverController = [[UIPopoverController alloc] initWithContentViewController:[appDelegateiPad naviPad]];
//RootViewController *r = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle: nil];
// popoverController = [[UIPopoverController alloc] initWithContentViewController:r];
//popoverController.popoverContentSize = CGSizeMake(320, 800);
//popoverController.delegate = self;
}
}
if (![popoverController isPopoverVisible]) {
[popoverController presentPopoverFromBarButtonItem:barButton3 permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
//[popoverController presentPopoverFromRect:CGRectMake(10, 10, 20, 20) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
}
else {
[popoverController dismissPopoverAnimated:NO];
}
}

Related

UIPopOverController issue

I am trying to display tableviewcontroller in a popover from a barbuttonitem like this :
- (IBAction)sortData:(id)sender {
if(!sortViewController)
sortViewController = [[SortDataViewController alloc] init];
[sortViewController.tableView setDelegate:self];
[sortViewController.tableView setTag:12];
[sortViewController setIsMatter:YES];
sortViewController.contentSizeForViewInPopover = CGSizeMake(150, 100);
sortViewController._radioSelection = 0;
[sortViewController.tableView reloadData];
}
if(!popOverController) {
popOverController = [[UIPopoverController alloc] initWithContentViewController:sortViewController];
}
[popOverController setPopoverContentSize:CGSizeMake(100, 100)];
[popOverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
for the first time it got displayed for 1sec and automatically dismissed and from next time onwards it is not displaying at all. Can anyone please help me in this regard.
Set delegate for the UIPopOVerController...
popOverController.delegate = self;
I am reloading the view after each second to test some requirement and forgot to disable it. So my view is reloading continuously and it's not giving enough time for popover to display it's view. Now I disabled it and the popover is working without any issues.

Closing a popover with a button (within another .xib file)

I've got a popover within my view. Within this popover there is content from another xib file (Infoscreen.xib). How can I dismiss the popover with a button which is inside another .xib file? Here's a snippit of my code:
-(IBAction)infoDruk: (id)sender {
if([popover isPopoverVisible]) {
[popover dismissPopoverAnimated:YES];
}
else {
Infoscreen *choser = [[Infoscreen alloc] init];
popover = [[UIPopoverController alloc]
initWithContentViewController:choser];
[choser release];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(230, 563);
[popover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
Help is greatly appreciated!
Your another xib should inform (give a call back) to your pop over that such button has been pressed. This concept is called delegates. Thus, you can dismiss the pop over in this call back method.

iPhone rotating view in navigationViewController mode

hours ago I post a question on organizing portrait and landscape mode in iPhone and now I think I know how to do it using willRotateToInterfaceOrientation:duration.
The first screen is 'Map View' with one button that leads to 'Setting View'. The Map View does not support rotate but for the Setting View I made separate view for portrait and landscape and they swap accordingly when rotated.
, ,
As you can see when Setting button pressed SettingView is added on the view stack as usual. So basically I use three view controllers; Setting, SettingLandscape and SettingPortrait.
I still found problem in rotating view in iPhone when I use navigationViewController. Segmented control is not working. it crashes without error message. It used to working fine without rotation.- when I'm not using multiple view for rotation-.
rotateViewController.m
This is root view controller.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(IBAction) buttonPressed{
Setting *settingViewController = [[Setting alloc] initWithNibName:#"Setting" bundle:[NSBundle mainBundle]];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController: settingViewController];
[self.navigationController presentModalViewController:navController1 animated:YES];
[settingViewController release];
[navController1 release];
}
Setting.m
This view controller does nothing but swap views when rotate and shows appropriate view between portrait and landscape.
In Setting.m, I swap view as follow;
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
NSLog(#"to Right");
SettingLandscape *setting_landscape = [[SettingLandscape alloc] initWithNibName:#"SettingLandscape" bundle:[NSBundle mainBundle]];
self.view = setting_landscape.view;
[setting_landscape release];
}
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
NSLog(#"to Left");
SettingLandscape *setting_landscape = [[SettingLandscape alloc] initWithNibName:#"SettingLandscape" bundle:[NSBundle mainBundle]];
self.view = setting_landscape.view;
[setting_landscape release];
}
if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
NSLog(#"to Portrait");
SettingPortrait *settingportrait = [[SettingPortrait alloc] initWithNibName:#"SettingPortrait" bundle:[NSBundle mainBundle]];
self.view = settingportrait.view;
[settingportrait release];
}
if (toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
NSLog(#"to PortraitUpsideDown");
SettingPortrait *settingportrait = [[SettingPortrait alloc] initWithNibName:#"SettingPortrait" bundle:[NSBundle mainBundle]];
self.view = settingportrait.view;
[settingportrait release];
}
}
In viewWillAppear, Setting view controller also has ;
self.title = #"Shell ";
self.navigationController.navigationBarHidden = NO;
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(Done)] autorelease];
and Done is
- (void) Done{
[self dismissModalViewControllerAnimated:YES];
}
SettingLandscape.m
This view stacked on when the view is rotated. This view controller has it's navigation bar.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = #"Setting Landscape";
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
in viewDidLoad;
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"landscape:viewDidLoad");
//self.title = #"SettingLandscape";//not working!!
//self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Done1" style:UIBarButtonItemStylePlain target:self action:#selector(Done)] autorelease];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
stringflag4MapType = [[NSString alloc] initWithString:#"blah"];
stringflag4MapType = [defaults stringForKey:#"flag4MapType"];
if (![stringflag4MapType isEqualToString:#"Hybrid"] && ![stringflag4MapType isEqualToString:#"Standard"] && ![stringflag4MapType isEqualToString:#"Satellite"]) {
segmentedControl4MapType.selectedSegmentIndex = 0;
}else if ([self.stringflag4MapType isEqualToString:#"Standard"]) {
segmentedControl4MapType.selectedSegmentIndex = 0;
}else if ([self.stringflag4MapType isEqualToString:#"Satellite"]) {
segmentedControl4MapType.selectedSegmentIndex = 1;
}else if ([self.stringflag4MapType isEqualToString:#"Hybrid"]) {
segmentedControl4MapType.selectedSegmentIndex = 2;
}
and following call does not get invoked. strange. doesn't matter rotation works anyway.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
NSLog(#"to Portrait");// does not print out.
SettingPortrait *settingportrait = [[SettingPortrait alloc] initWithNibName:#"SettingPortrait" bundle:[NSBundle mainBundle]];
self.view = settingportrait.view;
[settingportrait release];
}
if (toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
NSLog(#"to PortraitUpsideDown");
SettingPortrait *settingportrait = [[SettingPortrait alloc] initWithNibName:#"SettingPortrait" bundle:[NSBundle mainBundle]];
self.view = settingportrait.view;
[settingportrait release];
}
}
ok now, as you can see from those snap shots there are two navigation bar and each has its bar button, Done and Item. The Done button came from Setting and the Item button from SettingPortrait or SettingLandscape. All button's selector is same, that leads back to map view. The button Done works fine, but the button Item crashes. I need a button on navigation bar after rotation that acts like back button . I guess once I did 'self.view = settingportrait.view;' the problem starts.
The reason why I need the Item button work is that the segmented control started crashing once I add code to support rotation. If I found reason how to make the Item button-that is inside rotation view- work I think I can make the segmented control work as well.
You can download the whole code at https://github.com/downloads/bicbac/rotation-test/rotate-1.zip
https://github.com/downloads/bicbac/rotation-test/rotate-1.zip
this sample code is amazing for me. It solve my problem of rotating view just by simple delegate method
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
my best attempt to answer question without looking at code (don't have time tonight :( )
When you present setting viewcontroller modally, your top viewcontroller is setting.
When rotation happens, you load setting_landscape or setting_portrait viewcontroller, but only retain the view inside setting_landscape|portrait. Thus, setting_landscape/portrait viewcontrollers are released. When device is rotated, it's probably "setting" viewcontroller receiving rotation message, not the "setting_landscape/portrait" viewcontroller because they are not pushed on to the viewcontroller stack.
So, when you click on item or segment control, it will call delegate, which is probably set to setting_landscape|portrait which is released already.
What is the message in console you get with crash?
My recommendation would be to build setting viewcontroller with segmented control, then use "willAnimateRotationToInterfaceOrientation:duration:" function to reposition the segmented control to the right position and frame. Just by returning YES to all orientation, rotation should be supported, doesn't it?
What was the reason for using two separate viewcontroller for landscape/portrait? (I do this sometimes, but rarely)
Edit* you need to use "willAnimateRotationToInterfaceOrientation" callback to animate the changes, not "willRotate..."

UIPopoverController forces iPad into Portrait orientation

I think the issue here is that I'm trying to call a mediaPicker and that doesn't support other orientations...
Does anyone have a fix for this?
Here is my current code:
- (IBAction)openMediaPicker:(id)sender {
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = YES; // this is the default
mediaPicker.modalPresentationStyle = UIModalPresentationPageSheet;
//mediaPicker.prompt = #"Select items to play";
[self presentModalViewController:mediaPicker animated:YES];
[mediaPicker release];
// Init a Navigation Controller, using the MediaPicker as its root view controller
UINavigationController *theNavController = [[UINavigationController alloc] initWithRootViewController:mediaPicker];
[theNavController setNavigationBarHidden:YES];
// Init the Popover Controller, using the navigation controller as its root view controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:theNavController];
// Make a rect at the size and location of the button I use to invoke the popover
CGRect popOverRect = chooseMusicButton.frame;
// Specify the size of the popover
CGSize MySize = CGSizeMake(520.0, 720.0);
[popoverController setPopoverContentSize:MySize animated:YES];
// Display the popover
[popoverController presentPopoverFromRect:popOverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
popoverController.delegate = self;
}
This code is overly complicated. First you present the media picker modally, then you present it as a popover; why? In the popover, you stuff it into a navigation controller before presenting it; why? Presenting a media picker on iPad is much simpler than that:
MPMediaPickerController* picker =
[[[MPMediaPickerController alloc] init] autorelease];
picker.delegate = self;
UIPopoverController* pop =
[[UIPopoverController alloc] initWithContentViewController:picker];
self.currentPop = pop;
[pop presentPopoverFromRect:[sender bounds] inView:sender
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[pop release];
That works in any orientation and even survives rotation while the popover is showing.
All pre-defined modal controllers support all orientations but they must be presented from the root view controller for them to behave correctly in orientation and rotation. My guess is that that the "self" in your code is not the root view controller. You may have to re-architect the code a bit to make this happen if possible.
There are other hacks I have seen to make it work without being presented from the root view controller but they all seemed to be asking for trouble such as extending UIViewController with a category to over-ride interfaceOrientation.
If you can present it from the root view controller, it would be the simplest and cleanest but I realize it is not always possible (e.g., it is inside a library you are providing to third party apps to embed).

Dismissing UIPopoverController when using ABNewPersonViewController

I have an "add Contact" button which when on iPhone I present a navigation controller with root view controller of an ABNewPersonController modally.
If on iPad I have got a popover which I can display with the new person controller inside - nice.
The problem comes when trying to dismiss.
I can dismiss the popover when touching done or cancel within my implementation of didCompleteWithNewPerson using;
if(self.popoverController != nil)
[popoverController dismissPopoverAnimated:YES];
However, this doesn't dismiss when touching outside the popover.
I've returned YES for my popoverControllerShouldDismissPopover method and set the delegate of my popover to this. I've put an NSLOG inside this method and it's not dropping in there - Am I missing something?
Does anyone know how to dismiss the popover when touching outside?
Update - More Code
-(IBAction)contactsClicked:(id) sender{
ABNewPersonViewController *newPersonView = [[ABNewPersonViewController alloc] init];
[newPersonView setNewPersonViewDelegate:self];
[newPersonView setDisplayedPerson:newPerson];
UINavigationController *addContactNavController = [[UINavigationController alloc] initWithRootViewController:newPersonView];
[newPersonView release];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if(self.popoverController == nil){
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:addContactNavController];
self.popoverController = popover;
self.popoverController.delegate = self;
[popover release];
}
CGRect frame = [sender frame];
[popoverController presentPopoverFromRect:frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
} else {
[self presentModalViewController:addContactNavController animated:YES];
[addContactNavController release];
}
}
-(void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownPersonView didResolveToPerson:(ABRecordRef)person{
[self dismissModalViewControllerAnimated:YES];
}
-(void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person {
NSLog(#"DONE OR CANCEL clicked!!!!"); //prints
if (self.popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
}
[self dismissModalViewControllerAnimated:YES];
}
The Done and Cancel buttons of the new person controller work, dismissing the controller and the popover (when running on iPad). I guess this means the delegate for the ABNewPersonViewController is implemented correctly. (?)
I'm guessing that I may be confusing the issue by having multiple controllers and my popover delegate method is getting hidden or something?
Thanks in advance
EDIT - Delegate method
-(BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)thePopoverController{
NSLog(#"clicked outside the popover");//never prints
return YES;
}
From the docs:
Taps inside the popover window do not automatically cause the popover to be dismissed. Your view and view controller code must handle actions and events inside the popover explicitly and call the dismissPopoverAnimated: method as needed.
You should use the popover delegate methods –popoverControllerShouldDismissPopover: to listen for when it's about to be dismissed and do your saving etc. there.
Also, you should use self not this.