Hiding Keyboard in iphone resingFirstResponder Not working - iphone

I have create UITextField with alert view. After typing on UITextField I want to hide keyboard when user click "done" button.
This is my code. When I run it it's hitting the hideKeyBoard method (I found out using NSLog and the debugging). But this code not hiding the keyboard. Please Help me.
- (IBAction)doAlertInput:(id)sender
{
UIAlertView *alertDialog;
alertDialog = [[UIAlertView alloc]
initWithTitle: #"Please Enter Your Email Address!" message:#"You won’t see me"
delegate: self cancelButtonTitle: #"Ok" otherButtonTitles: nil];
userInput=[[UITextField alloc] initWithFrame: CGRectMake(12.0, 70.0, 260.0, 25.0)];
userInput.returnKeyType = UIReturnKeyDone;
[userInput addTarget:self action:#selector(hideKeyBoard) forControlEvents:UIControlEventEditingDidEndOnExit];
[userInput setBackgroundColor:[UIColor whiteColor]];
[alertDialog addSubview:userInput];
[alertDialog show];
[alertDialog release];
}
-(void)hideKeyBoard
{
[userInput resignFirstResponder];
NSLog(#"Key Board Hid");
}

I think you're missing the delegate for your UITextField
userInput.delegate = self;
also implement the protocol and that should be it. Cheers.

After appering the key board its covering the alert view. So what i am doing is shift up my alert view little higher. Then it will be not a problem because key board doesn't cover the alert view any more.
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
[myAlertView setTransform:myTransform];
The first parameter of CGAffineTransformMakeTranslation() is the X coordinate to move the origin of the view to. The second parameter is the Y location to move the origin of the view to. 130.0 doesn't exactly center the alert view between the status bar and the keyboard.
You'll need to add these two lines somewhere between the UIAlertView instantiation and the [UIAlertView show] line. I put them directly before the show line.
Also add the CoreGraphics framework to your project.
I think this will help you. Thank you :)

You can also use this method.
First set delegate method in your .h file
UITextfieldDelegate using <>
and then in your .m file.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Make sure do not forget to add textfield delegate,otherwise method -(BOOL) will not appear.

Related

Textfield in alertview not working in ios7?

I have an application in which I'm using a specific design for a reason. I put a text field in an alert view above an otherbutton with a background image. Everything is working fine in ios 6 version.
UIAlertView *av=[[UIAlertView alloc] initWithTitle:#"fdhdj" message:#" hdfjkhfjkhdk" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:#" ",#"cancel",nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
namefield = [[UITextField alloc] initWithFrame:CGRectMake(10.0,43.0, 264.0, 44.0)];
namefield.borderStyle = UITextBorderStyleNone;
namefield.background = [UIImage imageNamed:#"text_field_default.png"];
namefield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
namefield.textAlignment = UITextAlignmentCenter;
//[namefield setBackgroundColor:[UIColor whiteColor]];
[av addSubview:namefield];
[namefield release];
av.tag=12;
av.delegate=self;
[av show];
[av release];
But now in ios 7, I heard you can't easily alter the view hierarchy of a UIAlertView.
One alternative for this case is to set
alert.alertViewStyle = UIAlertViewStylePlainTextInput
But can we add that text field in wherever we want? As in my case above the first otherbutton.can anybody help me?
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Enter Student Name" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save",nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
i used to do like this ,and its working very fine
This is my component to support addSubview with alertView in iOS7.
CXAlertView - Custom alert-view which allow you to add view as main content.
The simple answer to your question is NO, you can't change anything in this testField for the UIAlertViewStylePlainTextInput and you shouldn't.
This is from Apple:
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
And unfortunately what you heard I heard you can't easily alter the view hierarchy of a UIAlertView is wrong, you cannot alter the view hierarchy of a UIAlertView in iOS7 at all.
There are a good alternative on the web, you can check in cocoacontrols.com
You can't easily alter the view hierarchy of a UIAlertView in iOS 7. (Nor should you; the documentation specifically tells you not to.) Head over to the developer forums to see a long discussion about it.
One alternative in your case is to set alert.alertViewStyle = UIAlertViewStylePlainTextInput; This will add a text field for you. You can access it in the UIAlertView delegate callback by using UITextField *textField = [alertView textFieldAtIndex:0];.

Dismiss the keyboard if action sheet is going to popup

I have a UIDatePickerView inside UIActionSheet as a input to the UITextField. When focusing on UITextField UIActionSheet will popup instead of Keyboard. When clicking on the done button in the UIActionSheet it'll hide. I have several other text fields behave as normal (Showing keyboard).
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if ([textField isEqual:txtExpDate]) {
[textField resignFirstResponder];
[self showDatePicker];
}
}
- (void) showDatePicker{
UIActionSheet *datePickerActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"Done" otherButtonTitles:nil];
datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 80, 0, 0)];
datePickerView.datePickerMode = UIDatePickerModeDate;
[datePickerActionSheet addSubview:datePickerView];
[datePickerActionSheet showInView:self.navigationController.view];
[datePickerActionSheet setBounds:CGRectMake(0, 0, 320, 500)];
[datePickerActionSheet release];
}
Now my problem is, Let say first user taps on normal textfield. Which will popups keyboard. Then without selecting done button he taps on date field. Which will popup action sheet (without dismissing the keyboard). After hiding actionsheet user has to tap on other text filed and click on return key of the keyboard.
I want to hide the keyboard if action sheet is going to popup?
Make the text field resignFirstResponder when you are going to bring up the action sheet. If you have more than one text fields, create an instance variable of type UITextField in .h file.
UITextField *currentTextField;
Then you can keep reference of the current text field in textFieldDidBeginEditing: method.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
currentTextField = textField;
....
And, call [currentTextField resignFirstResponder] in showDatePicker method.
- (void)showDatePicker {
[currentTextField resignFirstResponder];
UIActionSheet *datePickerActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"Done" otherButtonTitles:nil];
...
EDIT: #Samuel Goodwin is correct. We don't have to track the text fields. We can simply do the following to dismiss the keyboard.
- (void)showDatePicker {
[self.view endEditing:YES];
...
If you want to dismiss the keyboard for any reason, simply do:
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) target:nil forEvent:nil];
Worked for all text fields everywhere anywhere.
The method proposed by EmptyStack did not work for me (using xcode 4.2). I placed the message [currentTextField resignFirstResponder] in the method willPresentActionSheet:(UIActionSheet *)actionSheet, that also didn't work. Then I put the message in actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex and that did the trick... strange!
I know this is a very old post but I just thought I'd add the solution that worked for me (the others didn't). I found that I had to re-assign a text field to be first responder the resign it again. It doesn't matter which text field becomes then looses first responder status, this is just a way to ditch that keyboard.
[self.textfield becomeFirstResponder];
[self.textfield resignFirstResponder];
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([textField isEqual:txtExpDate]) {
[self showDatePicker];
return NO;
}
return YES;
}
It is working fine.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if(textField==myTextField2){
[myTextField1 resignFirstResponder];
[self showActionSheet];
return NO;
}
return YES;
}

UITextField inside an UIAlertView not working in iOS4.1 and works in 3.0

I have a UITextField added to an UIAlerView (Kind of adding user to list with nick name, and nickname is asked by the alertview.). Thats work perfect in iOS3, But I cant type to that UITextField in iOS4.1. in iOS4.1 I do get focus to that textfield inside alertview, and keyboard appears, however typing doesn't work.
Please help in this regard.
In iOS 4 UIAlertViews will automatically be moved and sized to avoid the keyboard if they contain any UITextFields in their subviews so you don't have to move it yourself. Simply add a text field like so:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello"
message:#"Tap below to enter text:\n\n"
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 68, 260, 30)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[alert addSubview:textField];
[textField release];
[alert show];
[alert release];
Be sure to add \n's to make room for your text field, you'll have to play around with getting the field in the correct position.
iOS4 broke all sorts of UIAlertView hacks (er... 'customizations').
I have a custom UIAlertView replacement class on github which supports an input (with UITextField) mode. You're welcome to try it out.
https://github.com/TomSwift/TSAlertView
I have created a post in my blog on the topic "How to add UITextField to UIAlertView from XIB".
To Solve your problem, you need to add a "fake" UITextField into the Alert view via coding. Please refer to the following link:
http://creiapp.blogspot.com/2011/08/how-to-add-uitextfield-to-uialertview.html

UIAlertView choice causing resignFirstResponder to fail

I'm having a similar issue to Anthony Chan's question, and after trying every suggested solution, I'm still stuck.
Somehow, only after interacting with my UIAlertView, I'm unable to dismiss the keyboard in another view of my app. It's as though the Alert is breaking my UITextField's ability to resignFirstResponder. Below I instantiate my UIAlertView, which then calls its didDismissWIthButtonIndex method. Then, I call the showInfo method, which loads another UIViewController.
UIAlertView *emailFailAlert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"error message text."
delegate:self
cancelButtonTitle:#"Not now"
otherButtonTitles:#"Settings", nil];
[emailFailAlert setTag:2];
[emailFailAlert show];
[emailFailAlert release];
Once the 'Settings' option is pressed, I'm calling this method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 2) {
if (buttonIndex == 1){
[self showInfo:nil];
}
}
}
My showInfo method loads the other ViewController, via the code below:
- (IBAction)showInfo:(id)sender {
FlipsideViewController *fscontroller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
fscontroller.delegate = self;
fscontroller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:fscontroller animated:YES];
[fscontroller release];
}
Upon clicking any textField in this Flipside VC, I'm unable to dismiss the keyboard as I normally can with - (BOOL)textFieldShouldReturn:(UITextField *)textField, and [textField resignFirstResponder]. I've omitted this code bc this question is getting long, but I'm happy to post if necessary.
The interesting part is that if I comment out the [self showInfo:nil] call made when the button is clicked and call it by clicking a test button (outside the alertView didDismissWithButtonIndex: method), everything works fine. Any idea what's happening here?
Thanks in advance!
When an alert, with more than one dismissal option, is called above a keyboard - the keyboard becomes un-dismissible with resignFirstResponder on the active textfield;
You will need to dismiss the keyboard before showing the alert.
Assuming your UITextField is called myTextField;
[myTextField resignFirstResponder]; //That's the only line I added
UIAlertView *emailFailAlert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"error message text."
delegate:self
cancelButtonTitle:#"Not now"
otherButtonTitles:#"Settings", nil];
[emailFailAlert setTag:2];
[emailFailAlert show];
[emailFailAlert release];
I hope this helps anyone who had to deal with this oddly obscure issue.
You should not call alertView:didDismissWithButtonIndex: directly. This delegate method will be executed automatically in all cases after the alert has disappeared. Otherwise the code will be run twice!

Dismissing the keyboard for a UITextField in UIAlertView

I have a Customized UIAlertView.I have a UITextField in that AlertView, which becomes the firstResponder as soon as it is shown. Now I need to dismiss the keyboard for that UITextField when user touches somewhere else in the AlertView. I have touchesBegan event for that.
Everything is in place and works fine, except when I call resignFirstResponder on the UITextField, it resigns from being the first responder but the keyboard is not dismissed. Is there any way to dismiss that keyboard.
I was looking for the solutions and found a similar post here with no answers
If anyone was able to find a way out please let me know. I even tried the following way, but its not working
UIWindow* tempWindow;
// Because we cant get access to the UIKeyboard throught the SDK we will just use UIView.
// UIKeyboard is a subclass of UIView anyways
UIView* keyboard;
// Check each window in our application
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
// Get a reference of the current window
tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
// Get a reference to the current view
keyboard = [tempWindow.subviews objectAtIndex:i];
// Loop through all views in the current window
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES){
[keyboard removeFromSuperview];
}
}
}
You must call resignFirstResponder on the UIAlertField,
eg:
[textField resignFirstResponder];[alertView resignFirstResponder];
Are you using custom code to show the keyboard? If not, try using the code from this StackOverflow question. I suspect that the resignFirstResponder message is not getting to the right control.
(The code in that answer is generally useful and sorely lacking from the SDK, IMHO.)
[self resignFirstResponder];
self means UIAlertView.
In my case it works.
There's no need to send a resignFirstResponder to the UIAlertView subclass. Just override becomeFirstResponder to return NO in UIAlertView subclass.
Apparently when you resign first responder on the UITextField, the UIAlertView becomes the next responder, keeping the Keyboard in place for some obscure reason.
Resigning that one too makes the keyboard disappear or better indeed override the becomeFirstResponder to return NO.
In my case this solution makes the UIAlertview animate to the upper left corner and animates back in place immediately after which looks very ugly and I cannot seem to find out why this is, but maybe someone else has some thoughts on that?
Here is my code extract in a class derived from UIAlertView:
- (BOOL)becomeFirstResponder
{
return NO;
}
- (void)someCustomButtonClicked:(id)sender
{
[textField resignFirstResponder];
// ...
}
-[UIView endEditing:]
http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/endEditing:
(Extra text because Stack Overflow requires at least 20 characters.)
Try these methods
-(void)viewDidLoad
{
[super viewDidLoad];
UIAlertView *loginAlert = [[UIAlertView alloc] initWithTitle:#"Welcome" message:#"Please Enter Login and Password" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[loginAlert textFieldAtIndex:0].delegate = self;
[loginAlert textFieldAtIndex:1].delegate = self;
[loginAlert setTag:777];
[loginAlert show];
[loginAlert release];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
And you should add the UITextFieldDelegate in the header file.
I met the problem just now.
Here's how I've solved the problem:
First, make your textField.delegate = self;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Title" message:nil delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"OK", nil, nil];
UITextField *oldPassWord = [alertView textFieldAtIndex:0];
oldPassWord.delegate =self;
UITextField *newPassWord = [alertView textFieldAtIndex:1];
newPassWord.delegate =self;
[alertView show];
Then, [textField resignFirstResponder];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[[alertView textFieldAtIndex:buttonIndex]resignFirstResponder];
}