This question already has answers here:
Showing a alertview with a textbox in iPhone
(4 answers)
Closed 9 years ago.
i can't get the value of textView from my alertView
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
[textView setText:#""];
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"put text\n\n\n\n\n\n" message:#"" delegate:self cancelButtonTitle:#"Annuler" otherButtonTitles:#"Envoyer",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];
it display alertview view with textview inside and its fine, i want to get the value of textview i my
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
}
can you help me please
Declare textView for use throughout your class
#implementation objectTables
{
UITextView *textView;
}
then in your
textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
[textView setText:#""];
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Push Notification\n\n\n\n\n\n" message:#"" delegate:self cancelButtonTitle:#"Annuler" otherButtonTitles:#"Envoyer",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];
and
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *textViewText = [textView text];
}
you can use - (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex which will return object of UITextField.
and from UITextField get text from it.
Or another way is
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
UITextField *TextField = (UITextField *)[alertView viewWithTag:YOURTAG];
}
You can try this :
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
[textView setText:#""];
textView.tag = 10;
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"put text\n\n\n\n\n\n" message:#"" delegate:self cancelButtonTitle:#"Annuler" otherButtonTitles:#"Envoyer",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];
In Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
UITextView *textView = (UITextView *) [alertView viewWithTag:10];
NSString *yourText = textView.text;
}
Just Declare Your UITextView in .h File
UITextView *textView;
and in .m file
textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
[textView setText:#""];
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"put text\n\n\n\n\n\n" message:#"" delegate:self cancelButtonTitle:#"Annuler" otherButtonTitles:#"Envoyer",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Gives you the Value of UITextView
NSLog(#"%#",[textView text]);
}
Or you can define it as the property in Extended class(.m file) and access it.
#Interface yourINterfaceName()
{
#property(nonatomic , retain ) UITextView *textView;
}
Try this
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
[textView setText:#""];
textView.tag =10;
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"put text\n\n\n\n\n\n" message:#"" delegate:self cancelButtonTitle:#"Annuler" otherButtonTitles:#"Envoyer",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextView *textView = (UITextView *)[alertView viewWithTag:10];
NSLog(#"%#",[textView text]);
}
Related
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);
}
}
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:.
i have created an alertview with textfield and the details which is inserted in textfield have to be accessed when i select a button.here is my code
-(IBAction)addcart:(id)sender{
UIAlertView *customAlertView = [[UIAlertView alloc] initWithTitle:#"Enter Your quantity" message:#"" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:#"Cancel", nil];
txtfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 40.0, 260.0, 20.0)];
[txtfield setBackgroundColor:[UIColor whiteColor]];
[txtfield setKeyboardType:UIKeyboardTypeNumberPad];
[customAlertView addSubview:txtfield];
[customAlertView show];
[customAlertView release]; }
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
if (buttonIndex == 0)
{
NSString* str=txtfield.text;
qtystr=str;
}
-(IBAction)showcontent:(id)sender{
NSLog(#"%#",qtystr); }
here qtystr,txtfield i've created property and synthesized it..But i dont know where i'm missing it...
If qtystr is a property, use self.qtystr when you reference it.
(Otherwise, use a breakpoint on qtystr=str; to see what the values are during the assignment.)
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.