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.
Related
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.
Im presenting and dismissing a Modal View Controller. I use delegation so I dismiss the modalView at the Parent.
- (void)launchDrawingSection{
drawingSectionViewController = [[DrawingSectionViewController alloc] init];
drawingSectionViewController.modalTransitionStyle = UIViewAnimationTransitionFlipFromLeft;
drawingSectionViewController.drawingModalDelegate = self;
[self presentModalViewController:drawingSectionViewController animated:YES];
}
- (void)didDismissDrawingModalView{
NSLog(#"didDismissDrawingModalView");
[drawingSectionViewController release];
[self dismissModalViewControllerAnimated:YES];
}
The app crashes after the dealloc method in the ModalView gets called.
Am I doing something wrong with the way I present and dismiss a Modal View Controller? Any idea?
Don't release before dismiss.
- (void)launchDrawingSection{
drawingSectionViewController = [[DrawingSectionViewController alloc] init];
drawingSectionViewController.modalTransitionStyle = UIViewAnimationTransitionFlipFromLeft;
drawingSectionViewController.drawingModalDelegate = self;
[self presentModalViewController:drawingSectionViewController animated:YES];
[drawingSectionViewController release];
}
- (void)didDismissDrawingModalView{
NSLog(#"didDismissDrawingModalView");
[self dismissModalViewControllerAnimated:YES];
}
Your fundamentals of Modal View Controller is not clear.
If you are using delegate protocols only to inform the drawing sections's parent control to dismiss the drawing section controller, then this is something useless. Because, the following thing does your job without use of delegates.
// Present drawing section.
- (void)launchDrawingSection{
drawingSectionViewController = [[DrawingSectionViewController alloc] init];
drawingSectionViewController.modalTransitionStyle = UIViewAnimationTransitionFlipFromLeft;
drawingSectionViewController.drawingModalDelegate = self;
[self presentModalViewController:drawingSectionViewController animated:YES];
[drawingSectionViewController release];
}
// (Put this in Drawing Section View Controller). This function dismisses drawing section.
- (void)dismissActionEvent{
// Drawing section view controller is asking its parent to dismiss it.
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
To understand clearly how presenting and dismissing modal view controller's work, refer to my answer here
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];
}
}
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.
I am very new to Obj-C and learning iphone development.
My question is how to add subview from app delegate.
Lets say I added subview called "MainView" from "applicationDidFinishLaunching" method.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
MainViewController *aViewController = [[MainViewController alloc] initWithNibName:#"MainView" bundle:nil];
self.mainViewController = aViewController;
[aViewController release];
[window addSubview:mainViewController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
"MainView.xib" file has a button to show its child view. When the button is clicked, it calls "showChildView" method.
- (IBAction)showChildView:(id)sender {
if (self.childViewController == nil) {
ChildViewController *childController = [[ChildViewController alloc] initWithNibName:#"ChildView" bundle:nil];
self.childViewController = childController;
[childController release];
}
[self.view insertSubview:childViewController.view atIndex:0];
}
From this code, when app launches, it shows "MainView" with a button. But when I clicked the button, the button is still visible as well as the content from the "ChildView.xib" file too.
How can I hide the "MainView" when I pressed the button and show only the contents of the "ChildView"?
Thanks for your help in advance.
well, you have to remove the original view first, before inserting the new subview, do it this way
- (IBAction)showChildView:(id)sender {
if (self.childViewController == nil) {
ChildViewController *childController = [[ChildViewController alloc] initWithNibName:#"ChildView" bundle:nil];
self.childViewController = childController;
[childController release];
}
[self.mainViewControlle.view removeFromSuperView];
[self.view insertSubview:childViewController.view atIndex:0];
}
Hope this helps.
You might want to check out the Utility App sample -- it demonstrates switching between two views with animation and adding/removing views from parent views.
you might want to create a navigation controller in the main view and than push the childviewcontroller onto it when invoking showChildView. You'll get the back navigation button for free that way