display popover when user click into text field? - objective-c

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.

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)
}

For those who want to display a popover, but do not want to display the keyboard when the text field is tapped, here is the solution that I've always used (Notice this is different than the previous answers textFieldShouldBeginEditing):
/*
* 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!

if your textField is inside a table cell your popover will point to the top of the screen because the frame of the textField frame is in reference to the view that contains the textfield. So you need to give it the correct view reference. You need to use the textField.superview as your view reference.
- (BOOL)textFieldShouldBeginEditing:(UITextField *) textField
{
...
[self.myPopover presentPopoverFromRect:textField.frame inView:textField.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
return NO;
}

Yes, there is a presentPopoverFromRect method.
To wire it up to the UITextField, you will need to implement the UITextFieldDelegate and call your showPopover code from the textFieldDidBeginEditing method.
The rect you use should be the rect of the TextField.

Related

While Tapping on UISearchBar control then it automatically calling shouldEndEditing Delegate of UISeachBar Controller

While Calling the UISearchBar Begin Editing then after the popover controller present then popover gets Disappear with hiding keyboard. It Calls the EndEditing without any ENTER Key Pressing
Below code shows the ShouldBeginEditing delegate of UISearchBar Controller
Sample Code:
-(BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar
{
if([searchBar isEqual:_normalSearch_SearchBar])
{
[[KeyboardHideShow getInstance] setScrollRectToThisFrame:[searchBar convertRect:searchBar.frame toView:self.scrollView]];
if(!self->_normalSearchPopOverController)
{
[[KeyboardHideShow getInstance] setUp_With_ScrollView:nil];
[self setTableViewController:[[UITableViewController alloc] initWithStyle:UITableViewStylePlain]];
[self->_tableViewController.tableView setDelegate:self];
[self->_tableViewController.tableView setDataSource:self];
[self addTransparentView_For_NormalSearch_And_AdvancedSearch];
//To add the transparent view
[self setNormalSearchPopOverController:[[UIPopoverController alloc] initWithContentViewController:self->_tableViewController]];
[self->_normalSearchPopOverController setDelegate:self];
[self->_normalSearchPopOverController setPopoverContentSize:CGSizeMake(POPOVERWIDTH,350)];
[self->_normalSearchPopOverController presentPopoverFromBarButtonItem:self->_searchBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
return YES;
}
}
return NO;
}
Kindly suggest where my code is wrong in the above example and feel free to ask any clarification regarding the code.
Code working Fine with Xcode v8 but not working with Xcode 10 and Xcode 11.

Need a really simple navigation controller with a table view inside a tab bar controller

I have an app with a tab bar controller (2 tabs). In one tab view controller, a button leads to an alert window. I want one button of the alert window to call a table view containing possible answers. I want that table view to have a done button and a title. I think that means a navigation controller has to be used. But most everything I can find on navigation controllers assumes a much more complicated situation. Here's part of the alert window logic:
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 2) {
AnswersViewController *aVC = [[AnswersViewController alloc] init];
[self presentViewController:aVC
animated:YES
completion:NULL];
}
}
And AnswersViewController looks like this:
#interface AnswersViewController : UITableViewController
#end
#implementation AnswersViewController
- (id) init
{
self = [super initWithStyle:UITableViewStylePlain];
return self;
}
- (id) initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self view] setBackgroundColor:[UIColor redColor]];
}
#end
This code all works as expected (an empty red UITableView appears).
Two questions I guess: 1. Is there a simple modification to what I have that can give me a done button and title in my table view? 2. If I have to go to a navigation controller (probably), how can I make a bare-bones navigation controller with a done button and title and embed the table view within it? Oh, and I want to do this programatically. And I think I prefer the done button and title to be in the navigation bar, no tool bar desired. Thanks!
To get what you are looking for, you do need to use a UINavigationController. That will provide the UINavigationBar where you can display a title and also buttons.
To implement this with a UINavigationController, you want to do smoothing like this (assuming you are using ARC, so you don't need to worry about memory management):
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 2) {
AnswersViewController *aVC = [[AnswersViewController alloc] init];
//Make our done button
//Target is this same class, tapping the button will call dismissAnswersViewController:
aVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissAnswersViewController:)];
//Set the title of the view controller
aVC.title = #"Answers";
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:aVC];
[self presentViewController:aNavigationController
animated:YES
completion:NULL];
}
}
Then you would also implement - (void)dismissAnswersViewController:(id)sender in the same class as the UIAlertView delegate method (based on the implementation I have here).
Hope this helps!

xcode adding buttons to navigation bar

I am making a simple app to display drink details, and now I am trying to add a view that allows the user to input their own drink. I already created a view to display the details, and now I am just passing the view into another controller to make the add drink view. Problem is, when I try to add a "cancel" and "save" button, it doesn't appear, although the code complies without any errors. I have attached code as reference.
This is the code that makes the new view, when the add button is pressed. (I made an add button that works, and it pulls up the nav bar)
- (IBAction)addButtonPressed:(id)sender {
AddDrinkViewController *addViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailSecond"];
UINavigationController *addNavController = [[UINavigationController alloc] initWithRootViewController:addViewController];
[self presentModalViewController:addNavController animated:YES];
NSLog(#"Add button pressed!");
This is the code from the addviewcontroller implementation file:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(save:)];
}
- (IBAction)save:(id)sender {
NSLog(#"Save Pressed");
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)cancel:(id)sender{
NSLog(#"Cancel Pressed");
[self dismissModalViewControllerAnimated:YES];
}
I have imported the header from the addview into the root controller, so I don't think that is the problem, do any of you guys see anything that's wrong?
Just change the line
[self presentModalViewController:addNavController animated:YES];
to
[self presentViewController:navigationController animated:YES completion:nil];
and see the magic. I also tested the code
My advice to you is to create a template for the view before you run through any code in the XIB file of your app. Rather than trying to set each button after allocating a brand new view, setting a new one in the XIB before-hand allows you to link each element with the app and make sure it looks just right before you debug.
Simply go into your "[Your-App-Name]viewController.xib" and drag a view from the objects library to the pane on the left. From here add each of your elements and position them where you want on the view. Now in the "[Your-App-Name]viewController.h" file, add IBOutlets for each element that you need to change, and add IBActions for each of the buttons. Also create an IBOutlet for the new view.
IBOutlet UIView* addDrinkView;
Back in the XIB file, use files owner to link each outlet to each element and each method to each button. Make sure you link the IBOutlet
Now in your "[Your-App-Name]viewController.m" file, you can define each button method and all you need to do to access the new view and dismiss it are the following:
-(IBAction)openAddView
{
[self setView:addDrinkView];
}
-(IBAction)saveButtonPressed
{
[self setView:view];
//save code goes here
}
-(IBAction)cancelButtonPressed
{
[self setView:view];
//cancel code goes here
}
This should be much easier than trying to position everything in code.
Hope this helps!

don't want to dismiss UIPopover

i am using this following code to display a popover in my View
imagePopOver = [[UIPopoverController alloc];
initWithContentViewController:self.photoLibraryImageCollection.imagePickerController];
imagePopOver.popoverContentSize = CGSizeMake(185,675);
imagePopOver.delegate = self;
[imagePopOver presentPopoverFromRect:CGRectMake(600,0, 140, 800)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
it working fine however if we click any other part of myView ,this displayed popover is dismissing.can any one tell me how can i avoid this problem. i don't want to dismiss it at any time.can any one tell me how can do it.
In the popover's delegate (your viewController, probably), implement
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController {
return NO;
}
Don't forget to set the delegate!
just wrote the below code at the time of popover initialization.
myPopOver.passthroughViews = [NSArray arrayWithObject:self.view];
in the above code will not dismiss your popOver and we can work with our View.
if you don't want to dismiss UIpopover only at the time of a textBox edit,simply write
myPopOver.passthroughViews = [NSArray arrayWithObject:self.textBox];

Problem with dismissing the keyboard when focus leaves a UITextView

I have 3 uitextfield in my project
I need to when tap inside one of them (uitextfield2 ) a custom subview appear , and need the key board to appear when tap on one another (uitextfield1 )
the problem is when I tab on uitextfield1 , the keypad appear and not go even I clicked return or tap on another uitextfield2
I need the keyboard to disappear when I click out of the uitextfield1 or when click return
I use the following code
- (BOOL)textFieldShouldReturn:(UITextField *)textField { // When the return button is pressed on a textField.
[textField resignFirstResponder]; // Remove the keyboard from the view.
return YES; // Set the BOOL to YES.
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// [textField resignFirstResponder];
[self SelectModalityClick]; // will be called If i tapped inside the uitextfield2 to display the custom view
return NO;
}
When you create your UITextField instances, you can set the inputView to whatever UIView subclass you want with something like this.
UITextField *aTextField = [[UITextField alloc] initWithFrame:aRect];
aTextField.inputView = myCustomInputView; // Set this up in your viewDidLoad method
aTextField.delegate = self;
aTextField.tag = MyCustomInputView; // #define this as some integer
[self.view addSubview];
[aTextField release];
You shouldn't need to do anything special to make the correct input view appear, if you have a UITextField instance variable for the first responder, just assign it in textFieldShouldBeginEditing: and resign its first responder status in textFieldShouldReturn:.