Resign is not working in UITextField of UIAlertView? - ios

i created alert view,in alert view one text field will come but when i click return button on keyboard it doesn't disappear,even i add the delegate to .h file.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(IBAction)barButtonPressed:(id)sender
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Enter Data" message:#"\n\n\n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
textUserName =[[UITextField alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 200.0f, 40.0f)];
textUserName.placeholder = #"Name";
textUserName.autocorrectionType = UITextAutocorrectionTypeNo;
textUserName.textAlignment=UITextAlignmentLeft;
textUserName.userInteractionEnabled = YES;
textUserName.enabled = YES;
textUserName.enablesReturnKeyAutomatically= NO;
textUserName.clearsOnBeginEditing = NO;
textUserName.borderStyle = UITextBorderStyleRoundedRect;
textUserName.keyboardType = UIKeyboardTypeDefault;
textUserName.delegate = self;
//[textUserName setReturnKeyType:UIReturnKeyDone];
**strong text**
[alert
addSubview:textUserName];
[alert show];
[self resignFirstResponder];
}
thank you

You can use UIAlertViewStylePlainTextInput of UIAlertView to get text field in alertView :
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Enter Data" message:#"\n\n\n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
// Set alertView style
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
textUserName =[alert textFieldAtIndex:0];
// Then customize your textField
textUserName.placeholder = #"Name";
textUserName.autocorrectionType = UITextAutocorrectionTypeNo;
textUserName.textAlignment=UITextAlignmentLeft;
textUserName.userInteractionEnabled = YES;
textUserName.enabled = YES;
textUserName.enablesReturnKeyAutomatically= NO;
textUserName.clearsOnBeginEditing = NO;
textUserName.borderStyle = UITextBorderStyleRoundedRect;
textUserName.keyboardType = UIKeyboardTypeDefault;
[alert show];
In this no need of setting delegate separately..

You need to set delegate property of UITextField after initializing it.
use:
textUserName =[[UITextField alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 200.0f, 40.0f)];
textUserName.delegate = self;

Related

Created 3 UITextField in UIAlertView but not getting action

I have created an UIAlertView with three (3) UITextField. How i can get the action of UIAlertView. The clickedButtonAtIndex delegate method is working.
But not getting the text i input in the field. Any idea ? Thanks in advance.
UIView *v_alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
v_alert.tag=10;
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = #"Your Previous Password";
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
textField1.tag=1;
[v_alert addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = #"Your New Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
textField2.tag=2;
[v_alert addSubview:textField2];
UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = #"Email";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
textField3.tag=3;
[v_alert addSubview:textField3];
alertV = [[UIAlertView alloc] initWithTitle:#"Change Password" message:#"" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[alertV setValue:v_alert forKey:#"accessoryView"];
alertV.tag==100;
alertV.delegate=self;
[alertV show];
In delegate :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"didDismissWithButtonIndex");
UITextField*txt1=[[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:0];
UITextField*txt2= [[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:1];
UITextField*txt3= [[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:2];
NSLog(#"%#--%#--%#",txt1.text,txt2.text,txt3.text);
}
Declare Your 3 TextField
UITextField *textField1, *textField2, *textField3;
Then replace this code with your code.
UIView *alertview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 250)];
alertview.backgroundColor = [UIColor whiteColor];
alertview.tag=10;
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = #"Your Previous Password";
textField1.secureTextEntry = YES;
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
textField1.tag=1;
[alertview addSubview:textField1];
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = #"Your New Password";
textField2.secureTextEntry = YES;
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
textField2.tag=2;
[alertview addSubview:textField2];
textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = #"Email";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
textField3.tag=3;
[alertview addSubview:textField3];
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:#"Change Password" message:#"" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[alertV setValue:alertview forKey:#"accessoryView"];
alertV.tag==100;
alertV.delegate=self;
[alertV show];
Now your UIAlertView Button Method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSLog(#"Button No click");
}
if (buttonIndex == 1) {
NSLog(#"Button Yes Cancel");
}
NSLog(#"%#--%#--%#",textField1.text,textField2.text,textField3.text);
}
Output :
Gems--Moradiya--Himmoradiya#gmail.com
First set textField delegate
<UITextFieldDelegate>
Then I tried the below code
NSString *strAddress = #"14 UPPER CIRCULAR ROAD, Singapore 058412";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:strAddress delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = #"Username";
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
[v addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = #"Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
[v addSubview:textField2];
UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = #"Address";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
[v addSubview:textField3];
[alert setValue:v forKey:#"accessoryView"];
[alert show];
TextField Delegate method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(#"The delegate method is called");
return YES;
}
Also see my screen shot
The output result is
The delegate method is called
That's the trick i accepted. But the standard way is for latest iOS:
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Change Password"
message:#""
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
UITextField *alertText1 = [alert.textFields objectAtIndex:0];
UITextField *alertText2 = [alert.textFields objectAtIndex:1];
UITextField *alertText3 = [alert.textFields objectAtIndex:2];
if(alertText1.text.length>0&&alertText1.text.length>0)
{
NSLog(#"Resolving UIAlert Action for tapping OK Button:%#-%#-%#",alertText1.text,alertText2.text,alertText3.text);
oldpPass=alertText1.text;
NewPass=alertText2.text;
[self update_password];
}
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"Resolving UIAlertActionController for tapping cancel button");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.placeholder = #"Enter old password";
textField.accessibilityIdentifier = #"paswordTextField";
textField.accessibilityLabel = #"passwordLabel";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.placeholder = #"Enter New password";
textField.accessibilityIdentifier = #"paswordTextField";
textField.accessibilityLabel = #"passwordLabel";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.accessibilityIdentifier = #"usernameTextField";
textField.placeholder = #"Enter email";
textField.text=self.emailTF.text;//
textField.accessibilityLabel = #"usernameLabel";
}];
[self presentViewController:alert animated:YES completion:nil];

UIAlertViewStyleLoginAndPasswordInput placeholder text won't set

I have an app that uses a UIAlertViewStyleLoginAndPasswordInput alert view. Everything works fine with the exception that the password field's placeholder text has some strange behavior. On my sim it refuses to change from 'Password' regardless of what I set it as. And Ive been having reports that it flip flops between 'Password" and the value I wish to set it at. This seems to be all iOS7 related.
I am attempting to set these values in the 'willPresentAlertView' delegate.
- (void)willPresentAlertView:(UIAlertView *)alertView
{
...
else if (alertView.tag == UIAlertViewTagInactivityTimeOut)
{
badgeNumberTextField = [alertView textFieldAtIndex:0];
verifyBadgeNumberTextField = [alertView textFieldAtIndex:1];
[badgeNumberTextField setPlaceholder:#"Badge Number"];
[verifyBadgeNumberTextField setPlaceholder:#"Verify Badge Number"];
[verifyBadgeNumberTextField setSecureTextEntry:NO];
[badgeNumberTextField setFont:[UIFont systemFontOfSize:ALERT_VIEW_TEXTFIELD_TEXT_FONT]];
[verifyBadgeNumberTextField setFont:[UIFont systemFontOfSize:ALERT_VIEW_TEXTFIELD_TEXT_FONT]];
[badgeNumberTextField setKeyboardType:UIKeyboardTypeNamePhonePad];
[verifyBadgeNumberTextField setKeyboardType:UIKeyboardTypeNamePhonePad];
[badgeNumberTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
[verifyBadgeNumberTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
[badgeNumberTextField setDelegate:self];
[verifyBadgeNumberTextField setDelegate:self];
...
//other code
}
}
Have you tried set the placeholder where you create the UIAlertView?
Try this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Login", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert textFieldAtIndex:0].placeholder = #"Username placeholder";
[alert textFieldAtIndex:1].placeholder = #"Password placeholder";
[alert show];

UITextField in input dialog not showing up

I am implementing a simple dialog which asks for a name. This is my code:
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:#"Enter Name"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 20.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
[nameField release];
It used to work fine but in iOS 7 the textfield doesn't show up. Can anyone help?
this will not work anymore in iOS7
try this instead
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(#"Enter a phone number",nil) delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel",nil) otherButtonTitles:NSLocalizedString(#"OK",nil), nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert setTag:1];
[[alert textFieldAtIndex:0] setDelegate:self];
[[alert textFieldAtIndex:0] resignFirstResponder];
[[alert textFieldAtIndex:0] setPlaceholder:NSLocalizedString(#"Enter Number here", nil)];
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alert textFieldAtIndex:0] becomeFirstResponder];
[alert show];
[alert release];
You can change your keyboard type for your requirement.
Good luck:)
Try to use like this...And there is no need to add text field as subview.
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:#"Enter Name"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 20.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
[nameField release];

UITextField inside UIAlertView cannot set cursor

Here is the code I have:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Confirm Address" message:#"Please confirm your address:" delegate:self cancelButtonTitle:#"Confirm" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.text = #"1234 ABC Street";
[alertTextField becomeFirstResponder];
UITextPosition *beginning = [alertTextField beginningOfDocument];
[alertTextField setSelectedTextRange: [alertTextField
textRangeFromPosition:beginning
toPosition:beginning]];
[alert show];
The cursor does not get set to the beginning of the textfield. What am i doing wrong?
Implement the UITextFieldDelegate on your Controller, and move your setSelectedTextRange code to the textFieldDidBeginEditing delegate callback:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Confirm Address" message:#"Please confirm your address:" delegate:self cancelButtonTitle:#"Confirm" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.text = #"1234 ABC Street";
[alertTextField becomeFirstResponder];
// Your UITextFieldDelegate implementer here
[alertTextField setDelegate:self];
[alert show];
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITextPosition *beginning = [textField beginningOfDocument];
[textField setSelectedTextRange:[textField textRangeFromPosition:beginning
toPosition:beginning]];
}
It is important you specify in your header file, or class extension that you implement <UITextFieldDelegate> and set the delegate as shown above.
Set delegate of alertTextField to current view controller, and perform setting range in textFields delegate method.
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Confirm Address" message:#"Please confirm your address:" delegate:self cancelButtonTitle:#"Confirm" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *alertTextField = [alert textFieldAtIndex:0];
alertTextField.text = #"1234 ABC Street";
alertTextField.delegate = self;
[alertTextField becomeFirstResponder];
[alert show];
TextField delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
UITextPosition *beginning = [textField beginningOfDocument];
[textField setSelectedTextRange:[textField
textRangeFromPosition:beginning
toPosition:beginning]];

How to create two textfields in an alert view, IOS

I want to create an alertview with two uitextfields inside of it.
method:
//show alertview for file input
- (IBAction)showAddFiles:(id)sender {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Enter File Details"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Add", nil];
UITextField *textFieldDescription = [message textFieldAtIndex:0];
textFieldDescription.placeholder = #"File Description : Ex. Acat Briefing";
UITextField *textFieldFileName = [message textFieldAtIndex:1];
textFieldFileName.placeholder = #"Exact File Name : Ex. acat.pdf";
[message show];
}
//make sure file description is long enoguh
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] <= 15 && [inputText length] >= 4)
{
return YES;
}
else
{
return NO;
}
}
//handle add button
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Add"])
{
UITextField *fileDescription = [alertView textFieldAtIndex:0];
UITextField *fileName = [alertView textFieldAtIndex:1];
NSLog(#"Desc: %#\nName: %#", fileDescription.text, fileName.text);
}
}
Error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textFieldIndex (0) is outside of the bounds of the array of text fields'
Why I do get this error, how can I create two uitextfields in an alert view?
=========Working Solution ===========
Thanks for the answer below works when you only need two plain textfields
//show alertview for file input
- (IBAction)showAddFiles:(id)sender {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Enter File Details"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Add", nil];
[message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
UITextField *fileDescription = [message textFieldAtIndex:0];
fileDescription.placeholder=#"Ex. acat.pdf";
[[message textFieldAtIndex:1] setSecureTextEntry:NO];
UITextField *fileName= [message textFieldAtIndex:1];
fileName.placeholder=#"Ex. Acat Briefing";
[message show];
}
After you allocated the "message" alert view. Add this to your code:
[message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[message textFieldAtIndex:1] setSecureTextEntry:NO];
This will make your alert view two text field inside.
The error you received occurs because there are no text fields in your UIAlertView, 'message'. The instance method "textFieldAtIndex" exists to access textFields in a UIAlertView that is created with a specific style, like UIAlertViewStylePlainTextInput, UIAlertViewStyleSecureTextInput, or UIAlertViewStyleLoginAndPasswordInput. These styles are set on the property "alertViewStyle". For example:
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
You may use "textFieldAtIndex" after setting this property, but unfortunately it looks as if none of these styles suit your needs.
What I've done before is to create a default styled UIAlertView (like you've already done), and add UITextFields as subviews to the UIAlertView.
For Example:
//Create the alert then add any labels and text fields as subviews.
//You can pad out an alertView by adding newline characters in the message. This will
// give the alertView more space to draw the text fields.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Two Text Field Alert"
message:#"\n\n\n\n\n"
delegate:self
cancelButtonTitle:#"CanceL"
otherButtonTitles:#"OK", nil];
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
[message addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(16,112,252,25)];
textField2.placeholder = #"Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
[message addSubview:textField2];
[message show];
[message release];
[textField2 release];
[textField1 release];
It's a lot more verbose and messy to do a login this way as opposed to the alertView styles, but you can adapt this as you see fit to add any number of subviews to an alert view.
Edited to simplify example.
You're getting that error because the UIAlertView does not contain any textfields. Since the alert view's textfield collection is empty when you try to call [alertView textFieldAtIndex:0], you end up with the NSInvalidArgumentException and a crash.
We are allowed to create only two text fields in alertview. Here:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Change Password"
message:#""
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UITextField * alertTextField1 = [alert textFieldAtIndex:0];
alertTextField1.keyboardType = UIKeyboardTypeDefault;
alertTextField1.placeholder = #"Type Current password";
[[alert textFieldAtIndex:0] setSecureTextEntry:YES];
UITextField * alertTextField2 = [alert textFieldAtIndex:1];
alertTextField2.keyboardType = UIKeyboardTypeDefault;
alertTextField2.placeholder = #"Type New Password";
[alert show];
Here is the solution for your question..
http://www.alterplay.com/ios-dev-tips/2009/12/username-and-password-uitextfields-in-uialertview-prompt.html

Resources