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]];
Related
When I have the following code the clickedButtonAtIndex delegate method is called:
UIAlertView * alertTextField = [[UIAlertView alloc] initWithTitle:#"Post To Facebook" message:#"What would you like the post to say?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Continue", nil];
alertTextField.tag = 2;
[alertTextField show];
But when i add in the UIAlertViewStylePlainTextInput in the delegate method is not called.
UIAlertView * alertTextField = [[UIAlertView alloc] initWithTitle:#"Post To Facebook" message:#"What would you like the post to say?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Continue", nil];
[alertTextField setAlertViewStyle:UIAlertViewStylePlainTextInput];
alertTextField.tag = 2;
[alertTextField show];
Why is this?
ensure that you added <UIAlertViewDelegate> in your view controller and your forget to add alertTextField.delegate = self; Then implement the delegate to receive the event.
UIAlertView * alertTextField = [[UIAlertView alloc] initWithTitle:#"Post To Facebook" message:#"What would you like the post to say?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Continue", nil];
[alertTextField setAlertViewStyle:UIAlertViewStylePlainTextInput];
alertTextField.delegate = self; // you forget to add this line
alertTextField.tag = 2;
[alertTextField show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
NSString *test = [[alertView textFieldAtIndex:0] text];
}
}
UIAlertView no longer available/support in iOS 8 and above so in this place use UIAlertViewController, example for how to use UIAlertViewController see this tutorial
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";
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];
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];
}
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