iOS multiple text fields on a UIAlertview In IOS 7 - ios

So the new iOS 7 has come out and I'm trying to add multiple textFields and labels to the UIAlertviews. I need three. I've been trying to add them as subviews and that doesn't work anymore. I have also tried to add multiple lines with the UIAlertViewStylePlainTextInput but it only seems to return one text field.
I need to add in labels to show them what to enter as well. Is there a way to accomplish this task with the new iOS 7?

The only solution i found using UIAlertView with more than one text field in iOS7 is for login only.
use this line to initialize your alertView
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
and this to grab the users input:
user = [alert textFieldAtIndex:0].text;
pw = [alert textFieldAtIndex:1].text
For other purposes than login view the other threads like this on: UIAlertView addSubview in iOS7

You can change accessoryView to any own customContentView in a standard alert view in iOS7
[alertView setValue:customContentView forKey:#"accessoryView"];
Note that you must call this before [alertView show].
Simplest illustrating example:
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"TEST" message:#"subview" delegate:nil cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
v.backgroundColor = [UIColor yellowColor];
[av setValue:v forKey:#"accessoryView"];
[av show];

If you want to add just two textfields to your UIAlertView, you can use UIAlertViewStyleLoginAndPasswordInput and modify the textfields as follows:
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Some Title" message:#"Some Message." delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:#"No, thanks", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert textFieldAtIndex:1].secureTextEntry = NO; //Will disable secure text entry for second textfield.
[alert textFieldAtIndex:0].placeholder = #"First Placeholder"; //Will replace "Username"
[alert textFieldAtIndex:1].placeholder = #"Second Placeholder"; //Will replace "Password"
[alert show];
Afterwards, in the UIAlertView delegate, you can simply get the text using:
text1 = [alert textFieldAtIndex:0].text;
text2 = [alert textFieldAtIndex:1].text;

Related

iOS - Keyboard hides UIAlertView with UITextfield

I have a UIAlertView with a UITextField. I want the user to type in something and tap OK. But when the keyboard appears, it hides the buttons of the UIAlertView. Is there a way to make the UIAlertView move when the keyboard is shown. Or just move it upward so the buttons will be seen by the user?
This is the code I use:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"title" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Other", nil] ;
alertView.tag = 2;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alertView textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypePhonePad;
CGRect frame = alertView.frame;
frame.origin.y = -140;
alertView.frame = frame;
alertTextField.text = #"some text";
[alertView show];
I dont know if alert view can be positioned itself, but maybe you can put it in to the scroll view and when user tap textfield it just scroll to the visible area for you.

Retrieving the value from UIAlertView textbox in iOS app

I have an iOS app that I recently updated to deal with the UIAlertView / SubView issue that causes the textboxes to render as clear or white (or not render at all, not sure which). In any case, this is a relatively simple question as I'm kind of new to Obj-C, but how do I get the value of the new textbox from another call in the app?
Here is my UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Password"
message:#"Enter your Password\n\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Login", nil];
alert.frame = CGRectMake( 0, 30, 300, 260);
It used to be stored as a UITextField, and then added to the UIAlertView as a subview:
psswdField = [[UITextField alloc] initWithFrame:CGRectMake(32.0, 65.0, 220.0, 25.0)];
psswdField.placeholder = #"Password";
psswdField.secureTextEntry = YES;
psswdField.delegate = self;
psswdField.tag = 1;
[psswdField becomeFirstResponder];
[alert addSubview:psswdField];
[alert show];
[alert release];
This is all commented out now, and instead I have it rewritten as:
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
This is how I used to retrieve the value:
[psswdField resignFirstResponder];
[psswdField removeFromSuperview];
activBkgrndView.hidden = NO;
[activInd startAnimating];
[psswdField resignFirstResponder];
[self performSelectorInBackground:#selector(loadData:) withObject:psswdField.text];
Now I'm a bit confused as to how I get the value from that textbox to send to loadData.
You don't want to add your own text field to the alert view. You're not supposed to directly add subviews to a UIAlertView. There is an alertViewStyle property on UIAlertView that you want to set to UIAlertViewStyleSecureTextInput, which will add a text field for you. So you would set it with a line like this:
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
Then you will retrieve the value in this text field using the delegate method - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex, which you must add to the class that you're setting as your UIAlertView delegate. Here is an example implementation of that delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
// Make sure the button they clicked wasn't Cancel
if (buttonIndex == alertView.firstOtherButtonIndex) {
UITextField *textField = [alertView textFieldAtIndex:0];
NSLog(#"%#", textField.text);
}
}

Alert view shifted upwards due to the addition of Text field in it

I am creating an Alert view on click of a button. I have placed a UITextfield in it. But the Alertview is shifted upwards in the screen. I am not able to bring it back at the center of the screen.
Here is my code and Screenshot.
and the code for creating this Alert view is this:-
- (IBAction)btn_cancelAppointment_click:(id)sender
{
// Here We are Creating Alertview which asks for
UIAlertView* cancelAlert = [[UIAlertView alloc] init];
[cancelAlert setDelegate:self];
[cancelAlert setTitle:#"Are you sure you want to request cancellation of this appointment?"];
[cancelAlert setMessage:#" "];
[cancelAlert addButtonWithTitle:#"NO"];
[cancelAlert addButtonWithTitle:#"YES"];
UITextField * txt_cancelNote = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 88.0, 245.0, 30.0)];
[txt_cancelNote setBackgroundColor:[UIColor whiteColor]];
[txt_cancelNote setBorderStyle:UITextBorderStyleRoundedRect];
[txt_cancelNote setPlaceholder:#"Enter cancellation note"];
txt_cancelNote.tag = 11;
[cancelAlert addSubview:txt_cancelNote];
cancelAlert.delegate = self;
[cancelAlert show];
}
So, if somebody can tell me how to bring it back to the center of the screen. Any help will be highly appreciated.
Give a try by setting the transform property of the UIAlertView
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Test" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
alertView.transform = CGAffineTransformMakeTranslation(x, y);

UIAlertView Textfield to Change UILabel Value

My app has a UILabel. I would like the user to be able to change the value of the label by pushing an "edit" button. I am able to implement a UIAlertView textfield with alert.alertViewStyle = UIAlertViewStylePlainTextInput, but I am not sure how the UILabel will receive the new value that was entered by the user.
This is what I have so far:
- (IBAction)edit
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Edit New Amount"
message:#"Enter new rate"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
UITextField *textField = [alert textFieldAtIndex:0];
textField.placeholder = #"Enter New Rate";
}
I also implemented the UIAlertViewDelegate protocol.
Assuming you want to change the label when the user presses "Ok" and have a reference to UILabel *someLabel as an ivar:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
if (buttonIndex != alertView.cancelButtonIndex) {
// UIAlertViewStylePlainTextInput will only ever have a single field at index 0
UITextField *field = [alertView textFieldAtIndex:0];
someLabel.text = field.text;
} else {
// this is where you would handle any actions for "Cancel"
}
}

UITextField in UIAlertView: clear button is not centered vertically in iOS 6.1

I use the following code to show a UIAlertView with a text field:
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Rename", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField* textField = [alertView textFieldAtIndex:0];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
[alertView show];
The result looks like this on iOS 5.1:
and on iOS 6.1:
As you can see, the clear button is a bit higher than it should be on iOS 6.1. Any idea how to properly center the button? Adding textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; does not help either.
(Apparently, this is a bug in iOS 6.1 but maybe someone knows a workaround. Besides, the height of the text field in alert views also seem to differ in different OS versions but that is another issue.)
This looks like a bug from Apple, but you can always add your own clear button in the textField, doing something like this :
First create a UITextField property:
#property (nonatomic, strong) UITextField *textField;
And then assign it to the alertView's textField
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Rename", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
self.textField = [alertView textFieldAtIndex:0];
self.textField.delegate = self;
//Create a custom clear button
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 19, 19)];
[button setImage:[UIImage imageNamed:#"clearButton.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(clearText) forControlEvents:UIControlEventAllTouchEvents];
[self.textField setRightView:button];
self.textField.rightViewMode = UITextFieldViewModeAlways;
And then in the clearText method,
- (void)clearText {
[self.textField setText:#""];
}
I tried this out and it works
Hope this helps
Another way to do this is by overriding UITextField's clearButtonRectForBounds:.

Resources