Reputation: 1204
I have a popup view (of a calculator) that is presented whenever a textfield begins to be edited. The code where the display method is called, and the display method itself are posted below.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//the background color may have been yellow if someone tried to submit the form with a blank field
textField.backgroundColor = [UIColor whiteColor];
sender = @"text field";
[self displayCalculator:textField.frame];
return YES;
}
The method that displays the view is :
-(IBAction)displayCalculator:(CGRect)rect{
calculator = [[CalculatorViewController alloc] initWithNibName:@"CalculatorViewController" bundle:nil];
popoverController = [[[UIPopoverController alloc] initWithContentViewController:calculator] retain];
[popoverController setPopoverContentSize:CGSizeMake(273.0f, 100.0f)];
[popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
My questions are:
1) How can I get the popover to stay? I would like the user to be able to click the textfield (the textfield that presented the popup in the first place), but when they do, the popup disappears.
2) The popup sometimes appears in such a way that it blocks the textfield, is there anyway that I can control where the popup appears? I am currently passing the frame of the textfield but that doesn't appear to be working
Upvotes: 1
Views: 161
Reputation: 18497
It's always good to check the docs to see if there are methods or properties which address a task or feature needed (UIPopoverController)
Seems like for your first issue you should take a look at the passthroughViews
property:
passthroughViews
An array of views that the user can interact with while the popover is visible. @property (nonatomic, copy) NSArray *passthroughViews Discussion
When a popover is active, interactions with other views are normally disabled until the popover is dismissed. Assigning an array of views to this property allows taps outside of the popover to be handled by the corresponding views.
For the second issue (covering the text area), you can offset the textField.frame to define a new CGRect
for the popoverController to use as its anchor.
CGRect targetRect = CGRectOffset(textField.frame, n, n);
Upvotes: 2
Reputation: 8608
(Problem 1)
In your displayCalculator method, you need to have a way to check if the popup is already being displayed. As of now, every time the textField updates you redraw the popup. You did to change the textFieldDelegate call to textFieldDidBeginEditing
.
Try this:
-(BOOL)textFieldDidBeginEditing:(UITextField *)textField {
//the background color may have been yellow if someone tried to submit the form with a blank field
textField.backgroundColor = [UIColor whiteColor];
sender = @"text field";
[self displayCalculator:textField.frame];
return YES;
}
-(IBAction)displayCalculator:(CGRect)rect{
//We don't want to continually create a new instance of popoverController. So only if it is nil we create one.
if (popoverController == nil)
calculator = [[CalculatorViewController alloc] initWithNibName:@"CalculatorViewController" bundle:nil];
popoverController = [[[UIPopoverController alloc] initWithContentViewController:calculator] retain];
[popoverController setPopoverContentSize:CGSizeMake(273.0f, 100.0f)];
}
//Check to make sure it isn't already showing. If it's not, then we show it.
if (!popoverController.popoverVisible) {
[popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
EDIT
As skinny pointed out (I should have mentioned). The popover will dismiss whenever you touch outside of it. That is why textFieldDelegate was changed to textFieldDidBeginEditing.
Here is a good tutorial that might be able to help you.
If all else fails just create your own popover.
Upvotes: 1