views:

292

answers:

1

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
A: 

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.

Ben Gottlieb
Well, it's still not working. It either crashes or I get a null value which is really weird. Hummm, any ideas why?
0SX
Nevermind, finally figured out the issue. Simulator was acting up. Thanks
0SX