Taking Text From a UIAlertView - ios

There seems to be something missing, but the below code is generating nil values for both title and title1 (even though it launches the right alert type correctly and doesn't indicate any warning or error). What could be the problem with this implementation of UIAlertView?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"High Score" message:(NSString *)scoreMessage delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
alert.alertViewStyle= UIAlertViewStylePlainTextInput;
UITextField *title1 = [alert textFieldAtIndex:0];
[alert show];
title1= [alert textFieldAtIndex:0];
NSString *title = title1.text;
NSLog(#"The name is %#",title);
NSLog(#"Using the Textfield: %#",[[alert textFieldAtIndex:0] text]);

Present alert somewhere in you code and set the view controller from it was presented as the delegate for your UIAlertView.Then implement the delegate to receive the event.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"High Score" message:#"Score Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
alert.alertViewStyle= UIAlertViewStylePlainTextInput;
[alert show];
Implement the delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
UITextField *textField = [alertView textFieldAtIndex:0];
NSString *title = textField.text;
NSLog(#"The name is %#",title);
NSLog(#"Using the Textfield: %#",[[alertView textFieldAtIndex:0] text]);
}

[alert show] returns immediately, before the user could have entered any text in the text field. You need to get the text after the alert has been dismissed by setting its delegate and implementing alertView:clickedButtonAtIndex: (for example, there are a couple of other possibilities).

In iOS 8 UIAlertview has been deprecated. UIAlertController is used instead of that.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Add Fruit" message:#"Type to add a fruit" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = #"Fruit";
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
UITextField *textFiel = [alert.textFields firstObject];
[fruits addObject:textFiel.text];
}];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];

You'll need to wait for the delegate callback alertView:clickedButtonAtIndex to get the text - keep a reference to your text field and show the alert, setting its delegate to self. When that method is called, the user has pressed the button to say they are finished entering text, so it's probably now safe to grab the text out of the text field.

Related

Display an Alert with Text Field Entry [duplicate]

This question already has answers here:
How do I access the input from my text field in UIAlertController with objective c?
(2 answers)
How to add text input in alertview of ios 8?
(9 answers)
Closed 6 years ago.
How to display a text entry field in an alert to get input from the user and use that input back in the application (display the input on a label ) ?
As showing below
Thank you for your time.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Enter Text"
message:#"Enter some text below"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *submit = [UIAlertAction actionWithTitle:#"Submit" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (alert.textFields.count > 0) {
UITextField *textField = [alert.textFields firstObject];
textField.text // your text
}
}];
[alert addAction:submit];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"something"; // if needs
}];
[self presentViewController:alert animated:YES completion:nil];
To add TextField to UIAlertView set alertViewStyle property with UIAlertViewStylePlainTextInput
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Done"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
In .h file add UIAlertViewDelegate as a protocol and implement the delegate method alertView:clickedButtonAtIndex in the .m file.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"%#", [alertView textFieldAtIndex:0].text);
}

Keyboard dismiss animation inside an UIAlertView

I have a problem with my iOS Application. Indeed, I have implanted an UIAlertView with an UITextField inside :
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message"
message:#"message"
delegate:self
cancelButtonTitle:#"Done"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
assert(textField);
textField.keyboardType = UIKeyboardTypeDecimalPad;
[alert show];
When I touch the "Done" button, there are no dismiss animation for the keyboard, it's not fluid... So I want to add an animation when the user touch the done button :)
Thanks in advance !
If your deployment target is iOS 8, you can work around this by using UIAlertController:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title"
message:#"Message"
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.keyboardType = UIKeyboardTypeDecimalPad;
}];
[alert addAction:[UIAlertAction actionWithTitle:#"Done" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];

UIAlertView Vs UIAlertController - no keyboard in iOS 8

I have a old Xcode project that is targeted for iOS 6 and higher. I recently opened it up in Xcode 6 and ran in iPhone 6 simulator for iOS 8. When I tried this action
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter Folder Name"
message:#"Keep it short and sweet"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
dialog.tag = 400;
[dialog show];
I get a pop-up window but when I click on the text field, no keyboard comes up. I googled and read that I need to use UIAlertController instead. Since I need to support iOS 6, 7 versions as well so I changed my code like this.
if ([UIAlertController class])
{
// use UIAlertController
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:#"Enter Folder Name"
message:#"Keep it short and sweet"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action){
//Do Some action here
UITextField *textField = alert.textFields[0];
NSLog(#"text was %#", textField.text);
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSLog(#"cancel btn");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"folder name";
textField.keyboardType = UIKeyboardTypeDefault;
}];
[self presentViewController:alert animated:YES completion:nil];
}
else
{
// use UIAlertView
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter Folder Name"
message:#"Keep it short and sweet"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
dialog.tag = 400;
[dialog show];
}
Again if I try the same action NO keyboard is displayed.
Is this a bug in Xcode 6 simulator or I am doing something wrong?
I Tested this, if iOS8 detects a hardware keyboard , than it does not open the keypad. As you might be testing in simulator , so it is not Showing any keypad
Press "Command" + "k" and you should be able to see the keypad.
When testing on a device you will not face this issue , unless user has connected his device with a hardware Bluetooth keypad , so this is not something you should worry about
Hope this helps
Another way of seeing the keyboard is setting a keyboard type. This could be an example code:
UIAlertController* alertController = [UIAlertController
alertControllerWithTitle:#"Test"
message:#"Testing keyboard"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler: ^(UITextField *tf)
{
tf.autocorrectionType = UITextAutocorrectionTypeYes;
tf.keyboardType = UIKeyboardTypeDefault;
}];
[self presentViewController:alertController animated:YES completion:nil];
Then you will see simulator's keyboard when you show the AlertController

Alertview with integer

Hello to everyone i would like to place my thinking about an alertview. I am thinking to create an Uialertview which will prompt the user to input two integers. Then i would like to retrieve these two integers and put the one on a timer and the second in an sql statement. So if anyone can help on how to implement that i would apreciate it. Thank you all.
Here is my code until now
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
NSLog(#"Entered: %#", [[alertView textFieldAtIndex:0] text]);
[alertView show];
You need to use delegate methods of UIAlterView.
First of all make "self a delegate" instead of nil.
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
Than add this function to your class:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
NSLog(#"%#", field.text);
}
To be 100% correct add also in your header class information that you implement this protocol .
// Try this
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[alertView textFieldAtIndex:0].delegate = self;
[alertView show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Entered: %#", [[alertView textFieldAtIndex:0]text]);
}

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

Resources