Dismissing UIPopoverController when using ABNewPersonViewController - iphone

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.

Related

Tab bar disappearing when switching controller

This code works perfectly to switch view controllers,except the tab bar controller disappears.
Now I try many different variations of this code
[self presentViewController:homeNavigationController animated:NO completion:nil];
But none of them seems to work properly. The push controller just freezes the view in place. Any tips on what to do?
- (void)_tabBarItemClicked:(id)item {
assert([item isKindOfClass:[UIButton class]]);
NSInteger selectedIndex = ((UIButton *)item).tag;
[self setSelectedIndex:selectedIndex];
[self _setSelectedItemAtIndex:selectedIndex];
NSDictionary *userInfo = #{#"index": [NSNumber numberWithInt:selectedIndex]};
[[NSNotificationCenter defaultCenter] postNotificationName:#"tabBarDidSelectItem" object:nil userInfo:userInfo];
if (selectedIndex ==2) {
HomeViewController *homeViewController = [[HomeViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *homeNavigationController = [[UINavigationController alloc] initWithRootViewController:homeViewController];
[self presentViewController:homeNavigationController animated:NO completion:nil];
}}
You're presenting homeNavigationController modally -- what you're seeing is the normal behavior of a modal view controller, that is, they take over the whole screen including the tab bar. If you want to see the tab bar, then don't use a modal presentation.
I solved this problem by:
1) Define YourTabBarController object in AppDelegate
2) And presentViewController from YourTabBarController's object instead of 'self' like
[appDelegateObj.yourTabBarObj presentViewController:homeNavigationController animated:NO completion:nil];];
Make sure your Appdelegate object is initialized.

Crashing after dismissing a Modal View Controller

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

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.

presenting viewController after contact has been selected in peoplePickerNavigationController?

I am having a little problem -
(BTW i have looked on
How can I present a modal view controller after selecting a contact?
but it didnt help me)
Basically i want to let the user select a contact using the - peoplePickerNavigationController. after the selection i want to presentModalViewController that will use the personRef data.
i can see that the "add person" method get called but the iphone does not present the view.
UPDATE - It works if i cancel the animation in the Dismiss dismissModalViewControllerAnimated and in the presentModalViewController, but then it looks pretty ugly.
this is the function called after the user selects the contact -
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)personRef {
TempREcordId = ABRecordGetRecordID(personRef);
BOOL hasDeleteDate = [GlobalFunctions CheckToSeeIfInHiBye:TempREcordId];
if (hasDeleteDate) {
[GlobalFunctions alert:NSLocalizedString(#"", #"") ];
}else{
[self addCustomValuesAfterSelection];
[self dismissModalViewControllerAnimated:YES];
}
return NO;
}
this is the function called but still the view is not presenting -
- (void)addPerson {
NSLog(#"#2");
AddViewController *addViewController = [[AddViewController alloc] initWithStyle:UITableViewStyleGrouped];
addViewController.delegate = self;
// Create a new managed object context for the new book -- set its persistent store coordinator to the same as that from the fetched results controller's context.
NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init];
self.addingManagedObjectContext = addingContext;
[addingContext release];
[addingManagedObjectContext setPersistentStoreCoordinator:[[fetchedResultsController managedObjectContext] persistentStoreCoordinator]];
addViewController.person = (Person *)[NSEntityDescription insertNewObjectForEntityForName:#"Person" inManagedObjectContext:addingContext];
addViewController.hiByeGroupId = [dataSource hibyeGroupId];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addViewController];
[self.navigationController presentModalViewController:navController animated:YES];
[addViewController release];
[navController release];
}
thank you very much.
Just don't dismiss the people picker and present your controller modally on top of it. If you afterwards dismiss your controller at some point dismiss the people picker instead (from the caller/parent) and your ViewController will be dismissed too.
From the Apple Docs:
dismissModalViewControllerAnimated: …
If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack.
Although by picking a person, the picker dismisses, the key is to dismiss it yourself in the delegate callback, after picking a person and than present your controller
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
ContactDetailViewController * vc = [[ContactDetailViewController alloc] initWithWithABRecord:person];
vc.delegate = self;
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self.navigationController presentViewController:nc animated:YES completion:^{
}];
}];
}
I suppose you just have to wait until the people picker disappeared by finishing its animation which it indicates by calling viewDidDisappear. If you override and hook up in there, you should be save to present your modal controller.

Problem with info view on iPhone app

I am currently using this code to bring up an info view for an iPhone app.
-(IBAction)showInfo:(id)sender {
InfoView *info = [[InfoView alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
info.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:info animated:YES];
[info release];
}
And this code go back to my main view
-(IBAction) exitInfo:(id)sender {
[self.parentViewController dismissModalViewControllerAnimated: YES];
}
When I run it on the simulator, it goes fine, however, on the actual iPhone, when I press the button that triggers the exitInfo method, it carries out its animation, but once it is completely off the screen and only my main view is visible, the screen will flicker, quickly showing the info view again. How can I fix this?
In my opinion, your viewController shouldn't even know it is modally viewed.
Try putting the code that send the dismissModalViewController message in your main controller and alert your main controller that the user clicked the dismiss button using a delegate.
something like this in your "parent" viewController
-(IBAction)showInfo:(id)sender {
InfoView *info = [[InfoView alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
info.modalTransitionStyle = UIModalTransitionStylePartialCurl;
info.delegate = self;
[self presentModalViewController:info animated:YES];
[info release];
}
-(IBAction)closedButtonClicked {
[self dismissModalViewControllerAnimated: YES];
}
and in your "Modal" viewController
-(IBAction) exitInfo:(id)sender {
[delegate closedButtonClicked];
}
Of course you will need a protocol like
#protocol MyCustomDelegate
-(IBAction)closedButtonClicked;
#end
and something like this in your "modal" interface
-id<MyCustomDelegate> delegate;