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