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"
}
}
Related
Some of my ViewController.h
NSString *title1;
NSString *sub1;
#interface MapMain : UIViewController
{
IBOutlet UIButton *setter;
}
All within the same ViewController.m
-(IBAction)Write:(id)sender
{
[self alertView];
}
-(void)alertView
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#""
message:#"Your Info"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Done", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UITextField * alertTextField1 = [alert textFieldAtIndex:0];
alertTextField1.keyboardType = UIKeyboardTypeDefault;
alertTextField1.placeholder = #"Name";
UITextField * alertTextField2 = [alert textFieldAtIndex:1];
alertTextField2.keyboardType = UIKeyboardTypeDefault;
alertTextField2.placeholder = #"Username";
alertTextField2.secureTextEntry = NO;
[alert show];
}
So this code correctly makes a pop up appear with two text fields when I click on a specific button. How can I store the text the user inputs when they press the "Done" button into my NSString *title1 and NSString *sub1 respectfully, so that I can use the string values when other buttons are pressed later. And how can I make my program not store the text when the user presses the "Cancel" button?
Make your MapMain class conform to the UIAlertViewDelegate protocol
#interface MapMain : UIViewController <UIAlertViewDelegate>
and then implement the delegate's method for when the alert is dismissed like this
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) //only if cancel is not clicked (done is clicked)
{
UITextField * alertTextField1 = [alertView textFieldAtIndex:0];
UITextField * alertTextField2 = [alertView textFieldAtIndex:1];
//... do whatever else you need to here like
title1 = alertTextField1.text;
}
}
I am currently using the following to use a popup alert which allows the user to set a delay in seconds;
The input itself works fine, but the number is not remembered, so if I put 5, or 10, when I press 'set' it just ignores that value and goes to instant recording again. here is the IBAction to call the setDelay, and below it, I have posted the Alertview
-(IBAction)setDelay:(id)sender
{
// open a alert with text field, OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Set delay in seconds" message:#" "
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Set", nil];
CGRect frame = CGRectMake(14, 45, 255, 23);
if(!delayTextField) {
delayTextField = [[UITextField alloc] initWithFrame:frame];
delayTextField.borderStyle = UITextBorderStyleBezel;
delayTextField.textColor = [UIColor blackColor];
delayTextField.textAlignment = UITextAlignmentLeft;
delayTextField.font = [UIFont systemFontOfSize:14.0];
delayTextField.backgroundColor = [UIColor redColor];
delayTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
delayTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; // use the default type input method (entire keyboard)
delayTextField.returnKeyType = UIReturnKeyDefault;
delayTextField.delegate = self;
delayTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
}
else
{
delayTextField.text=#"";
}
alert.delegate=self;
[alert addSubview:delayTextField];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert show];
[alert release];
}
//
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag==100)
{
if (buttonIndex!=alertView.cancelButtonIndex)
{
[self featureButtonPressed];
}
}
else
{
if (buttonIndex==alertView.cancelButtonIndex)
{
self.delayTextField.text=#"";
}
}
}
From iOS 7 you can't add subviews to UIAlertView anymore -
UIAlertView addSubview in iOS7.
But if you need just simple UITextField in your UIAlertView you can just set UIAlertView style to UIAlertViewStylePlainTextInput and retrieve value from that field later. Eg.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Set delay in seconds" message:#" "
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Set", nil];
alert.delegate=self;
[alert addSubview:delayTextField];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert show];
then in your delegate method:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag==100)
{
if (buttonIndex!=alertView.cancelButtonIndex)
{
[self featureButtonPressed];
}
}
else
{
if (buttonIndex==alertView.cancelButtonIndex)
{
UITextField *alertViewTextField = [alertView textFieldAtIndex:0];
//Do something with alertViewTextField.text value
}
}
}
#Guferos answer is absolutetly correct. It doesn't work because iOS 7 doesnt support this.
But in case you need more than a simple TextView, you can use a framework such as https://github.com/jdg/MBProgressHUD
:)
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);
}
}
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;
If I tap an image,can I pop up the UITextfield for entering the data? How would I do that?
You could use a UIAlertView as follows:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Please enter your text:"
delegate:self
cancelButtonTitle:#"Done"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
textField.keyboardType = UIKeyboardTypeDefault;
textField.placeholder = #"Enter some text";
[alert show];
Edit: to handle the returned text, implement the delegate method
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex > 0) {
UITextField *textField = [alert textFieldAtIndex:0];
NSString *text = textField.text;
if(text == nil) {
return;
} else {
//do something with text
}
}
}
You could add a UITapGestureRecognizer to your UIImageView .. on its selector action you add a textField to your view .. just in that simple your implmentation will be. and don't forget to set userInterActionEnabled = YES for the imageView since the default value is NO.