Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display popover when user click into text field?

Hi i have been following a book a how to display the popover when the user clicks on a toolbar button item. It works fine but I want to display the popover when user clicks into a textField. It seems like it would be some minor adjustment. Like changing the IBAction "showPopover" method a bit. This is what the code looks like for that method:

- (IBAction)showPopover:(id)sender{

    if(popoverController == nil){   //make sure popover isn't displayed more than once in the view
        popoverController = [[UIPopoverController alloc]initWithContentViewController:popoverDetailContent]; 

    [popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    popoverController.delegate = self;
    }
}

There is a another instance method other than "presentPopoverFromBarItem" that is called "presentPopoverFromRect".Would I use that instead? I tried to write the code for it but I'm not sure how to relate it to my TextField or how draw the rectangle needed.Can anyone help me with this?Thanks.

like image 536
serge2487 Avatar asked Nov 29 '22 17:11

serge2487


1 Answers

you have to use the textfields delegate method textViewShouldBeginEditing:

Something like this:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if(popoverController == nil){   //make sure popover isn't displayed more than once in the view
        popoverController = [[UIPopoverController alloc]initWithContentViewController:popoverDetailContent]; 
    }
    [popoverController presentPopoverFromRect:textView.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    popoverController.delegate = self;
    return NO; // tells the textfield not to start its own editing process (ie show the keyboard)
}
like image 86
Matthias Bauch Avatar answered Dec 10 '22 09:12

Matthias Bauch