Display popover when user clicks in text box?

Hi, I read a book on how to display a popover when a user clicks a button on a toolbar. It works fine, but I want to display a popover when the user clicks on a text element. It looks like it will be a small adjustment. How to change IBAction The showPopover method is a bit. Here is the code for this 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 another instance method other than "presentPopoverFromBarItem" which is called by "presentPopoverFromRect". Should I use this instead? I tried writing code for it, but I'm not sure how to link it to my TextField or how to draw the rectangle needed. Can anyone help me with this? Thanks.

+4
source share
4 answers

you need to use the textViewShouldBeginEditing: delegation method textViewShouldBeginEditing:

Something like that:

 - (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) } 
+9
source

For those who want to display a popover but don’t want to display the keyboard while listening to a text field, here is the solution I always used (note that this is different from the previous textFieldShouldBeginEditing answers):

 /* * Handle when text field is about to start edit mode */ - (BOOL)textFieldShouldBeginEditing:(UITextField *) textField { // Create popover controller if nil [self.myPopover presentPopoverFromRect:textField.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; return NO; } 

Hope this helps!

+8
source

Yes, there is a presentPopoverFromRect method.

To connect it to a UITextField, you need to implement UITextFieldDelegate and call the showPopover code from the textFieldDidBeginEditing method.

You must use the TextField rectangle.

+1
source

if your textField is inside the table cell, your popover will point to the top of the screen, because the frame frame of the textField refers to the view containing the text field. Therefore, you need to give it the correct link to view. You need to use textField.superview as a reference to your view.

 - (BOOL)textFieldShouldBeginEditing:(UITextField *) textField { ... [self.myPopover presentPopoverFromRect:textField.frame inView:textField.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; return NO; } 
+1
source

Source: https://habr.com/ru/post/1341077/


All Articles