I have a textfield in the alertview called phoeNumberTF.
phoneNumberTF.delegate = self;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Enter your phone number" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:#"Send"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSArray * textfields = alert.textFields;
phoneNumberTF = textfields[0];
[self sendPhoneNumber:phoneNumberTF.text];
}]];
shouldChangeCharactersInRange delegate method is not getting called
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
}
phoneNumberTF is not a part of UIAlertController so no need of delegate for this phoneNumberTF.delegate = self;
add the configuration delegate to your UIAlertController
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Enter your phone number" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.delegate = self;
}];
for more info you get the sample here
Related
I want to allow users to copy/cut but mainly PASTE data from other apps into my app. I need to allow them to paste data in UIAlertView, when they are logging to the app. How can i make it?
This is my code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:[NSString stringWithFormat:NSLocalizedString(#"enter_login", nil)]
delegate:self
cancelButtonTitle:#"Ok"
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeEmailAddress];
[[alert textFieldAtIndex:1] becomeFirstResponder];
[UserDefaultsServices resetLoginCredentials];
[UserDefaultsServices resetLoginData];
self.alertView = alert;
[alert show];
It does this:
show what my code does, I need to allow users to paste data in password TextField
You can use this answer for swift versions :
Objective C version of shared answer is :
- (IBAction)showAlert:(id)sender {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Add New Name" message:#"Your message" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = #"Name";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = #"Password";
textField.secureTextEntry = true;
}];
UIAlertAction *saveAction = [UIAlertAction actionWithTitle:#"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// here you can access your textfields' texts
NSString *textField1 = alertController.textFields[0].text;
NSString *textField2 = alertController.textFields[1].text;
NSLog(#"Saving %# %#", textField1, textField2);
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// here you can access your textfields' texts
NSString *textField1 = alertController.textFields[0].text;
NSString *textField2 = alertController.textFields[1].text;
NSLog(#"Canceled %# %#", textField1, textField2);
}];
[alertController addAction:saveAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
Above code work like this :
I have a UIAlertController, which contains a textfield and 2 buttons: OK and Cancel. i want to retrieve the text that the user entered in the textfield when he presses OK, but because the textfield is in a block i cant access the textField.text field that is inside the block from the OK button block. how do i access it and use the text entered by the user? see my code below.
- (IBAction)saveItinerary:(id)sender
{
NSString *itineraryNameInput = [[NSString alloc] init];
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Save Itinerary"
message:#"Enter Name for the Itinerary"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"Name";
textField.text = itineraryNameInput;
}];
NSLog(#"name = %#", itineraryNameInput);
[self presentViewController:alert animated:YES completion:nil];
}
Use the textFields property of the UIAlertController.
UITextField *textField = alert.textFields.firstObject;
I'm using UIAlertController for the first time. I need to prompt the user for an input. Anyway, the following is what I have.
- (void)showAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Hello!" message:#"Type something.") preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *noButton = [UIAlertAction actionWithTitle:#"Cancel") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
UIAlertAction *yesButton = [UIAlertAction actionWithTitle:#"Enter") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//NSString *input = alert.textFields[0].text;
//NSLog(#"input was '%#'", input);
UITextField *textField = alert.textFields[0];
NSLog(#"%#",textField.text); // <====== It's not returned
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
[textField addTarget:self action:#selector(alertTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[yesButton setEnabled:NO];
}];
[alert addAction:noButton];
[alert addAction:yesButton];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)alertTextFieldDidChange:(UITextField *)sender {
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController) {
UITextField *textfield = alertController.textFields.firstObject;
UIAlertAction *okAction = alertController.actions.lastObject;
okAction.enabled = [self validateName2:textfield.text:5]; // function for validating user input
}
}
For some reason, when the user tap Yes, the application doesn't return user input. In fact, Xcode dashes 'text' as in 'NSString *input = alert.textFields[0].text. A strange thing is that UIAlertView doesn't return user input. That's why I've switched to UIAlertController.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 101) {
if (buttonIndex == 1) {
NSString *username = [[alertView textFieldAtIndex:0] text];
NSLog(#"%#",username); // <====== It's never returned.
}
}
}
Anyway, I wonder what I'm doing wrong with UIAlertController?
Muchos thankos
p.s. I'm using Xcode 6.4 (6E35b). This issue may be caused by Xcode. If I debug the code with the iPad2 simulator, it'll actually return user input while it doesn't if I use the iPhone4S simulator.
Use this for textField in alertcontroller
__block typeof(self) weakSelf = self;
UIAlertController *alertController;
alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.tag = 1001;
textField.delegate = weakSelf;
textField.placeholder = #"";
[textField addTarget:weakSelf action:#selector(alertTextFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
}];
//OK Button
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
UITextField *textField = alertController.textFields.firstObject;
// Do stuff here
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
- (void)alertTextFieldDidChange:(UITextField *)sender
{
alertController = (UIAlertController *)self.presentedViewController;
if (alertController)
{
UITextField *textField = alertController.textFields.firstObject;
UIAlertAction *okAction = alertController.actions.lastObject;
okAction.enabled = textField.text.length > 0; // condition to enable button
}
}
#pragma mark - UITextField delegate methods
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
I would recommend using UITextFieldDelegate to handle the textField!
Set textField.delegate = self and then use the - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string method to do what you do in alertTextFieldDidChange if the replacementString has a length of > 0.
You are provided with delegate methods for UITextField so you don't have to create the selectors yourself, I recommend you use them to their fullest!
Here are the docs in case you want to know more! https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/
I need to put 5 UITextField in UIAlertView. I use this project:
https://www.cocoacontrols.com/controls/aralertcontroller
This is my code:
ARAlertController *alert = [ARAlertController alertControllerWithTitle:#"My Alert" message:#"This is an alert." preferredStyle:ARAlertControllerStyleAlert];
ARAlertAction* defaultAction = [ARAlertAction actionWithTitle:#"OK" style:ARAlertActionStyleDefault handler:^(ARAlertAction * action) {}];
[alert addAction:defaultAction];
[alert presentInViewController:self animated:YES completion:nil];
Can someone help me to implement the 5 UITextField in my (ARAlertController *)alert ?
Thanks
You have to use something like:
ARAlertController *alert = [ARAlertController alertControllerWithTitle:#"My Alert" message:#"This is an alert." preferredStyle:ARAlertControllerStyleAlert];
ARAlertAction* defaultAction = [ARAlertAction actionWithTitle:#"OK" style:ARAlertActionStyleDefault handler:^(ARAlertAction * action) {}];
[alert addAction:defaultAction];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"First";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"second";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"third";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"fourth";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"fifth";
}];
[alert presentInViewController:self animated:YES completion:nil];
Good luke.
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.