Hey guys, I'm having trouble obtaining the value/text from a AlertView TextField. Maybe someone can look at my code and see what I'm doing wrong. Every time I press OK, the label get's a (null) value. I must be missing something here.
TextFieldINAlertViewController.h
#import <UIKit/UIKit.h>
#interface TextFieldInAlertViewController : UIViewController {
UITextField *myTextField;
IBOutlet UILabel *labelView;
NSString *FieldStr1;
}
#property (nonatomic, copy) NSString *FieldStr1;
#end
TextFieldInAlertViewController.m
#import "TextFieldInAlertViewController.h"
#implementation TextFieldInAlertViewController
#synthesize FieldStr1;
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter Name Here" message:#"blank" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"OK!", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alert setTransform:myTransform];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
}
-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)index
{
if (index == 0){
return;
}
if (index == 1){
FieldStr1 = myTextField.text;
labelView.text = [NSString stringWithFormat:#"%#" , FieldStr1 ];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
myTextField is a local variable in viewDidLoad, but it's also a member variable in your class. Get rid of the local declaration, and you should be fine. Also, I'd move this code to viewDidAppear:, rather than viewDidLoad.
Since iOS5 you can use property on the alertView which is a much simplier way to add a textField.
If you want to add a TextField to an UIAlertView, you can use this property (alertViewStyle) for UIAlertView:
- (void)showAlert{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
((UITextField *) [alertView textFieldAtIndex:0]).text = #"Default Value";
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"%#", [alertView textFieldAtIndex:0].text);
}
Check UIAlertView reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html
Hugues
Related
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
warning: 'UIAlertView' may not respond to '-addTextFieldWithValue:label:'
I am using below code for put textfield in AlertView, it gives a warning like "UIAlertView may not respond - addTextFieldWithValue: label:" & "UIAlertView may not respond - textFieldAtIndex"...
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email" message:#""
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Submit", nil];
[alert addTextFieldWithValue:#"" label:#"Email"];
// Username
textfieldName = [alert textFieldAtIndex:0];
textfieldName.keyboardType = UIKeyboardTypeEmailAddress;
textfieldName.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldName.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
please help!
there is no method "addTextFieldWithValue" for uialertview
if you need to modify your alert view have a look at my blog here - http://www.makebetterthings.com/blogs/iphone/add-uitextfield-in-uialertview/
These is no such methods like addTextFieldWithValue in UIAlertView.
[alert addTextFieldWithValue:#"" label:#"Email"]; is a private api. My app got rejected because of using this.
Use this
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Twitter"
message:#"\n\n\n\n"
delegate:self
cancelButtonTitle:#""
otherButtonTitles:#"", nil];
myAlert.frame = CGRectMake( 0, 0, 300, 260);
textfieldName = [[UITextField alloc] initWithFrame:CGRectMake(13, 95, 260, 25.0)];
[textfieldName setBackgroundColor:[UIColor whiteColor]];
textfieldName.placeholder = #"xyz";
textfieldName.keyboardType = UIKeyboardTypeAlphabet;
textfieldName.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldName.autocorrectionType = UITextAutocorrectionTypeNo;
textfieldName.delegate = self;
textfieldName.tag = 1;
[myAlert addSubview:textfieldName];
**I use in my project < and use and implement
use in .h file**
#protocol InputAlertViewDelegate
-(void)handleEnteredValue:(NSString*)_value;
-(void)handleCancellation;
#end
#interface InputAlertView : UIAlertView <UIAlertViewDelegate> {
UITextField *textField;
id<InputAlertViewDelegate> caller;
} #property(nonatomic, retain) UITextField *textField;
#property(nonatomic, retain) id<InputAlertViewDelegate> caller;
-(NSString*) theText;
-(void) prepare;
-(void) makePassword;
-(void) makeTelephone;
+(InputAlertView*) inputAlertViewWithTitle:(NSString*)_title initialFieldText:(NSString*)_text caller:(id<InputAlertViewDelegate>)_caller;
**use in .m file**
+(InputAlertView*) inputAlertViewWithTitle:(NSString*)_title initialFieldText:(NSString*)_text
caller:(id<InputAlertViewDelegate>)_caller
{
InputAlertView *_alert =
[[[self alloc] initWithTitle:_title message:#"\n" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil] autorelease];
_alert.delegate = _alert;
_alert.caller = _caller;
[_alert prepare];
_alert.textField.text = _text;
return _alert;
}
-(void)prepare
{
self.textField = [[[UITextField alloc] initWithFrame:CGRectMake(12.0, 45, 260.0, 30.0)] autorelease];
[textField setBackgroundColor:[UIColor clearColor]];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
[self addSubview:textField];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
[self setTransform:myTransform];
-(void) show{
[textField becomeFirstResponder];
[super show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){ [self.caller handleCancellation];
} else{
[self.caller handleEnteredValue:[self theText]];
}
}
i'm working on an app, but i have some problems with auto dismiss alert view and switch to another views.
Here is my code:
This is file "Progress.h":
#import <Foundation/Foundation.h>
#import "Progress_ToolViewController.h"
#interface Progress : UIAlertView {
UIProgressView *myPro;
//UILabel *myPercent;
UILabel *myCount;
NSTimer *timer;
int count;
BOOL userDismissed;
}
#property (nonatomic, retain)UIProgressView *myPro;
//#property (nonatomic, retain)UILabel *myPercent;
#property (nonatomic, retain)UILabel *myCount;
-(void)start;
-(void)moreprogress;
-(void)makecount;
-(id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;
#end
Here is file "Progress.m":
#import "Progress.h"
#import "Progress_ToolViewController.h"
#implementation Progress
#synthesize myPro;
#synthesize myCount;
//#synthesize myPercent;
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
{
if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
{
UIProgressView *thePro = [[UIProgressView alloc] initWithFrame:CGRectMake(12.0, 60.0, 220.0, 20.0)];
UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(240.0, 55.0, 60.0, 20.0)];
[thePro setBackgroundColor:[UIColor clearColor]];
[theLabel setBackgroundColor:[UIColor clearColor]];
[theLabel setTextColor:[UIColor whiteColor]];
theLabel.text = 0;
[self addSubview:thePro];
[self addSubview:theLabel];
self.myPro = thePro;
self.myCount = theLabel;
myCount.text = #"0";
[self start];
[thePro release];
[theLabel release];
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 65.0);
[self setTransform:translate];
}
return self;
}
-(void)start{
myPro.progress = 0.0;
//myPro.text = #"%";
timer = [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:#selector(moreprogress) userInfo:nil repeats:YES];
timer = [NSTimer scheduledTimerWithTimeInterval:0.20 target:self selector:#selector(makecount) userInfo:nil repeats:YES];
}
-(void)moreprogress{
myPro.progress = myPro.progress + 0.02;
if(myPro.progress == 1.0){
[timer invalidate];
[self dismissAlert];
}
}
-(void)makecount{
count = count + 4;
myCount.text = [[NSString alloc] initWithFormat:#"%i", count];
if(count == 100){
[timer invalidate];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 1) {
if (buttonIndex == 1) {
UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:#"Sub-Alert" message:#"You've chosen \"Sure\"." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[subAlert show];
[subAlert release];
}
else if (buttonIndex == 2) {
UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:#"Sub-Alert" message:#"You've chosen \"Not sure\"." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[subAlert show];
[subAlert release];
}
}
else if ([alertView tag] == 2) {
userDismissed = YES;
}
}
-(void)dismissAlert{
if(userDismissed) return;
[self release];
}
#end
This is file "Progress_ToolViewController .h":
#import <UIKit/UIKit.h>
#class Progress;
#interface Progress_ToolViewController : UIViewController <UIAlertViewDelegate>{
BOOL userDismissed;
Progress *prog;
}
- (void)showAlert;
#end
This is file "Progress_ToolViewController.m":
#import "Progress_ToolViewController.h"
#import "Progress.h"
#implementation Progress_ToolViewController
- (void)showAlert
{
prog = [[Progress alloc] initWithTitle:#"Your Value:" message:#" " delegate:self cancelButtonTitle:nil okButtonTitle:nil];
prog.tag = 2;
userDismissed = NO;
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(dismissAlert) userInfo:nil repeats:NO];
[prog show];
}
-(void)dismissAlert{
if(userDismissed) return;
[prog dismissWithClickedButtonIndex:0 animated:YES];
[prog release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)alertView:(Progress *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 1) {
if (buttonIndex == 1) {
UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:#"Sub-Alert" message:#"You've chosen \"Sure\"." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[subAlert show];
[subAlert release];
}
else if (buttonIndex == 2) {
UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:#"Sub-Alert" message:#"You've chosen \"Not sure\"." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[subAlert show];
[subAlert release];
}
}
else if ([alertView tag] == 2) {
userDismissed = YES;
}
}
- (void)dealloc {
[super dealloc];
}
#end
When i run this app, it's also dismiss but it turns off the program. How can i solve it?
-(void)showAlert
{
prog = [[UIAlertView alloc]initWithTitle:#"Auto alert" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(dismissAlert) userInfo:nil repeats:NO];
[prog show];
}
-(void)dismissAlert
{
[prog dismissWithClickedButtonIndex:0 animated:YES];
TrackingSetting *_obj = [[TrackingSetting alloc]init];
[self presentViewController:_obj animated:YES completion:nil];
}
Try this sample code it is working fine i have tested and after diss miss of alert you can create another view object and and show over it no issue.
But in your case i think "prog" is a view .so you have to remove that view from superview means it depends on how you are showing that view if you are adding that view using add sub view you have to remove from super view .if you are using present modal view then you have to dismiss that view .
How can I dismiss UIAlertView ? This code doesn't work.
#property (nonatomic, retain) UIAlertView *activityAlertView;
- (void)viewDidLoad
{
self.activityAlertView = [[UIAlertView alloc] initWithTitle:#"Receiving data" message:#"\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil, nil];
[activityAlertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
-(void) myfunc
{
[self alertView:activityAlertView clickedButtonAtIndex:1];
}
The - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated method of the UIAlertView class does what you want. eg:
[myAlertView dismissWithClickedButtonIndex:-1 animated:YES];
Since you are using a delegate callback method of UIAlertView, I think it is better you use the following code
[myAlertView dismissWithClickedButtonIndex:0 animated:YES];
if not then use the above suggested code
[myAlertView dismissWithClickedButtonIndex:-1 animated:YES];
I'm trying to add a new cell to a tableview, and pop up an alert with a UITextField to allow the user to input the title they wish to give the new cell. I have code to pop up an alert with a UITextField when the "+" button is pressed, and the code to add a new cell, however I don't know how to get the text from the UITextField to insert it into the cell's title.
This is my code to pop up the alert:
UIAlertView* alertPopUp = [[UIAlertView alloc] init];
[alertPopUp setDelegate:self];
[alertPopUp setTitle:#"Enter event title"];
[alertPopUp setMessage:#" "];
[alertPopUp addButtonWithTitle:#"Cancel"];
[alertPopUp addButtonWithTitle:#"OK"];
UITextField * eventNameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[eventNameField setBackgroundColor:[UIColor whiteColor]];
[alertPopUp addSubview:eventNameField];
[alertPopUp show];
and my alertView action is:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:#"Cancel"]) {
return;
}
else if([buttonTitle isEqualToString:#"Ok"]) {
}
}
What can I do to get the text from eventNameField when "Ok" is pressed and add it to a mutablearray named eventList? Thanks!
Set the tag on eventNameField to something meaningful
eventNameField.tag = 1001;
Then inside of the alertViewDelegate you can get the TextField by using - [UIView viewWithTag:]
UITextField* textField = (UITextField*)[alertView viewWithTag:1001];
eventNameField.text should give you the value
//declare the array
NSMutableArray* eventList = [NSMutableArray array];
//set its value
[eventList addObject:eventNameField.text];
DHamrick probably has the better solution, but found one more that could work.
You can get all the subviews inside alertView with [alertView subviews] (returns a NSCFArray), then you just have to find the one that is of class UITextField, in your case you could get it with:
[[[alertView subviews] objectAtIndex:4] text]
The old answers don't take advantage of changes in UIAlertView.
In iOS 5 and beyond, there is an easier way of using UITextfield on an alert view:
UIAlertView *alertPopUp = [[UIAlertView alloc] initWithTitle:#"Enter event title" message:#"" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
alertPopUp.alertViewStyle = UIAlertViewStylePlainTextInput;
self.alertTextField = [message textFieldAtIndex:0];
self.alertTextField.keyboardType = UIKeyboardTypeAlphabet;
alertPopUp.delegate = self;
[alertPopUp show];
[self.alertTextField becomeFirstResponder];
where alertTextField was set up like this:
#propery (nonatomic, strong) UITextField *alertTextField;
Then you can access alertTextField in your delegate response:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:#"Cancel"]) {
return;
}
else if([buttonTitle isEqualToString:#"Ok"]) {
NSLog(#"your text string is %#", self.alertTextField.text);
}
}
You could also just give the alertView a unique tag, and compare tag numbers, rather than save a reference to it using #property.
Here is the code I have to create an UIAlertView with a textbox.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter A Username Here" message:#"this gets covered!"
delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"OK!", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alert setTransform:myTransform];
alert.tag = kAlertSaveScore;
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[myTextField release];
My question is, how do I get the value from the textfield in:
- (void) alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
}
I know I can get the standard stuff for the alertview such as actionSheet.tag and such, but how would I get the above created textfield?
#interface MyClass {
UITextField *alertTextField;
}
#end
And instead of declaring it locally, just use that.
//...
alertTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
//...
- (void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *text = alertTextField.text;
alertTextField = nil;
}
Just give it a tag, and find it using the tag later. So, using your code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter A Username Here" message:#"this gets covered!"
delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"OK!", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alert setTransform:myTransform];
alert.tag = kAlertSaveScore;
// Give the text field some unique tag
[myTextField setTag:10250];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[myTextField release];
Then, in the call-back, wherever that happens to be and without having to worry about memory management or state management of the text field:
- (void) alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Get the field you added to the alert view earlier (you should also
// probably validate that this field is there and that it is a UITextField but...)
UITextField* myField = (UITextField*)[actionSheet viewWithTag:10250];
NSLog(#"Entered text: %#", [myField text]);
}