UIAlertViewStyleLoginAndPasswordInput placeholder text won't set - ios

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];

Related

Can we programmatically insert text in UIAlertView's textfield, iOS7

Can we programmatically insert text in UIAlertView's textfield, iOS7
Created UIAlertView with textinput in IOS7.
Can anyone help me with "inserting text programatically in textfield"?
Hopefully this will give answer for your question.
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.delegate = self;
alertView.title = #"Enter Info";
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView addButtonWithTitle:#"Cancel"];
[alertView addButtonWithTitle:#"OK"];
[alertView textFieldAtIndex:0].text = #"My Text";
[alertView show];
You can achieve this by using this code
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"" message:#"whatever you like" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:#"Cancel", nil];
alert.alertViewStyle=UIAlertViewStylePlainTextInput;
[alert textFieldAtIndex:0].text=#"Hello";
[alert show];
and result will be like this:
Please try to use like this..
UIAlertView* av = [[UIAlertView alloc] init];
[av setDelegate:self];
[av setTitle:#"Hi"];
[av setMessage:nil];
[av addButtonWithTitle:#"Cancel"];
[av addButtonWithTitle:#"OK"];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].text = #"Text";
You can use text property to set text inside textfield.
textFiled.text = #"YOUR TEXT";

UIAlertView with one text field (for login purpose)

When i pull the tableView downwards, an alertView should appear with a login text field.
The alert has to come but the textField is not to be selected.
Once i click on this textField, then the keyboard should appear.
For this i am using this code:
- (void)toggleCells:(UIRefreshControl*)refreshControl
{
message = [[UIAlertView alloc] initWithTitle:#"Create a new list"
message:#""
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message setTag:1001];
//[message textFieldAtIndex:0].delegate = self;
[refreshControl beginRefreshing];
[message show];
[refreshControl endRefreshing];
//...
}
try: (not so nice but a quick-fix)
message = [[UIAlertView alloc] initWithTitle:#"Create a new list"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message setTag:1001];
[[message textFieldAtIndex:0] setDelegate:self]; //you'll surely need it later
[message show];
[[message textFieldAtIndex:0] resignFirstResponder]; //after showing the alertView
NOTE: The method above ain't really nice.
Alternative Method:
NOTE: declare "BOOL skipAlertTextField;" in the .h
- (void)toggleCells:(UIRefreshControl*)refreshControl {
//...
message = [[UIAlertView alloc] initWithTitle:#"Create a new list"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message setTag:1001];
[[message textFieldAtIndex:0] setDelegate:self];
skipAlertTextField = YES; //important
[message show];
//...
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if(textField.tag == 1001 && skipAlertTextField) {
skipAlertTextField = NO;
return NO;
}
return YES;
}
Try this
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"SayWhat" message:#"Please enter Email address" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av setAlertViewStyle:UIAlertViewStylePlainTextInput];
[[av textFieldAtIndex:0] setPlaceholder:#"Email address"];
[[av textFieldAtIndex:0] setDelegate:self];
[av show];
[[av textFieldAtIndex:0] resignFirstResponder];

UIAlertView textfield keyboard type

I having problem in displaying the textfield keyboard type in the alertview. I want to display numberpad keyboard but it not working.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (alertView.tag) {
case 1:
if (buttonIndex == 0) {
UITextField *textField = [alertView textFieldAtIndex:0];
textField.keyboardType = UIKeyboardTypeNumberPad;
[self performSelectorOnMainThread:#selector(ProcessDeleteTransaction:) withObject:textField.text waitUntilDone:NO];
}
break;
case 2:
[self.navigationController popToRootViewControllerAnimated:YES];
break;
default:
break;
}
}
Try this:
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"Howdie" message:#"msg" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
a.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [a textFieldAtIndex:0];
assert(textField);
textField.keyboardType = UIKeyboardTypeNumberPad;
[a show];
I'm not really sure why you're posting code for your UIAlertViewDelegate method. Why not configure the keyboard type when the UIAlertView is created?
I actually had never tried this before but had a minute to give it a shot, and this code works fine:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Test" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
textField.keyboardType = UIKeyboardTypeNumberPad;
[alert show];
As you know, - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex is a delegate method called when the user clicks a button on an alert view, but the textField.keyboardType should be set before becomeFirstResponder..so you should set keyboardType property once [[UIAlertView alloc] init]
Try this:
- (void)willPresentAlertView:(UIAlertView *)alertView {
[[alertView textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeDecimalPad];
[[alertView textFieldAtIndex:0] becomeFirstResponder];
}

Resign is not working in UITextField of UIAlertView?

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;

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