iphone MFMailComposeViewController - iphone

Is there a way to dismiss the keyboard from MFMailComposeViewController ? If the user rotates the device, I am loading a separate controller without "send" or "cancel" being pressed, and the keyboard is remaining on screen. Is there an way to dismiss the keyboard without "done" or "send" being pressed?

You can find the first responder and ask it to resign active which should dismiss the keyboard.
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:#selector(firstResponder)];
[firstResponder resignFirstResponder];

Related

why UITextField becomeFirstResponder doesn't show keyboard in my child viewcontroller?

In my first ViewController, I make one UITextField to becomeFirstResponder, and the focus stay that TextField and keyboard appear automatically. It just works fine. But then I present another child ViewController which has 3 UITextFields (A,B,C) in this ViewController, I set the first UITextField (say A) to becomeFirstResponder in this child ViewController, the focus stay the A but the keyboard doesn't appear. I searched all the answers the whole day, but nothing found.
Here is the code.
in parent VC, I set:
TestVC *testVC= [[TestVC alloc] init];
testVC.modalPresentationStyle=UIModalPresentationFormSheet;
[detailvc presentViewController:testVC animated:NO completion:nil];
in child VC TestVC, I set:
UITextField *v_matcode=[MyViewUtil addTextField:#"" x:10 y:10 width:100 height:40];
[self.view addSubview:v_matcode];
[v_matcode becomeFirstResponder];
and this is the addTextField():
+(UITextField *) addTextField:(NSString *)text x:(int)_x y:(int)_y width:(int)_width height:(int)_height {
UITextField *v_txt = [[UITextField alloc] init];
v_txt.frame= CGRectMake(_x, _y, _width, _height);
v_txt.borderStyle = UITextBorderStyleRoundedRect;
return v_txt;}
The v_matcode has the focus, but the keyboard doesn't appear. But if I don't set this textfield becomeFirstResponder and let user tap it,the keyboard appear correctly. Why is that?
try setting animated to YES
[detailvc presentViewController:testVC animated:YES completion:nil];

resignFirstResponder for UITextField with search returnkey

Simple question: how can I resign my textfield if my "done" key is Search. Essentially, what do I do if the user does not want to search, but instead wants to cancel...
thanks
You can use: a cancel button for SearchBar and need to implement this SearchBar delegate :
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
isSearching = NO; //This is a flag which specifies if searching is going on or not.
searchBar.text = #""; //Clears out search bar text
[self resetSearch]; //Reset search resets all the flags and variables.
[self.leadsTable reloadData]; //Reloads the tableView
[searchBar resignFirstResponder]; //Resigns responder from Search bar
}
This is a proper way to resign the responder if user doesn't want to search.
Look at how you can add an in-built Cancel button in UISearchBar. Check the property "Shows Cancel Button" (Red Arrow highlight)
EDIT:
STEP-1:
Check conditionally whether your textField's text is blank? If so resignFirstResponder for the TextField. You need to set the Delegate to self for the UITextField using code:
txtField.delegate = self;
Or you can do the same from the interface builder by connecting TextField's delegate to File's Owner.
STEP-2: You need to implement this delegate method.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if([textField.text isEqualToString:#""])
{
[textField resignFirstResponder];
}
return YES;
}
EDIT-1:
Now create a UIToolbar with one bar button labeled 'Cancel'
Once you have done that:
You can write an button click event for UIToolBar:
-(IBAction)cancelClicked:(id)sender
{
[txtField resignFirstResponder];
}
Once you have done that you can now just write:
txtField.inputAccessoryView = toolbarOutlet;
Hope this helps you.
The text on the return button is irrelevant to the discussion. You want to know how to resign first responder without pressing the return button, and there are a few ways to do it.
Use the inputAccessoryView of the text field to display a separate cancel button in a toolbar above the keyboard.
Use a tap gesture recognizer on the field's superview to recognize when the user taps outside the field, and call [self.view endEditing:YES] (where self is your view controller). This will cause the first responder to resign. (This is very finicky in a scroll view.)
Swap out the rightBarButtonItem of the current view controller for a cancel bar button item while editing, assuming you have a UINavigationBar on screen at the time. When editing ends, swap back in the regular right bar button item, if any.

Why is the keyboard not showing when view loads?

I have this code in my viewdidload:
[_txtName setDelegate:self];
[_txtName becomeFirstResponder];
_txtName.enabled = YES;
_txtName.text = #"";
But when my view loads, the keyboard does not show, any idea why?
_txtName is the UITextField
As pointed out in a comment by #lukya you should place the call to becomeFirstResponder in either viewWillAppear: or viewDidAppear:. If you place the call in viewWillAppear: the keyboard will be shown on screen when the view is shown. If you would like the keyboard to animate in from the bottom when the view appears you should put the call in viewDidAppear:.

How to overlay on top of the Keyboard in xcode

How do you overlay something, ie a UIImageView over the keyboard which pops up when editing a UITextFIeld?
You can catch keyboard window using following code, if you put it into textFieldDidBeginEditing method:
UIWindow* keyboard = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

Make cancel button lower keyboard

I have a UISearchBar. I enabled the 'cancel' button so it says cancel right next to the search box how can i set that button to simply lower the keyboard when the user presses it?
For this there's one delegate method called when cancel button is pressed
-(void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
[searchBar resignFirstResponder];
}
-Happy Coding....