2009-12-30 3 views
0

Эй, ребята, мне трудно получить значение/текст из AlertView TextField. Может быть, кто-то может посмотреть на мой код и посмотреть, что я делаю неправильно. Каждый раз, когда я нажимаю OK, метка get имеет значение (null). Я должен что-то упустить.AlertView с проблемой TextField

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 

ответ

0

MyTextField является локальной переменной в viewDidLoad, но это также переменная-член в классе. Избавьтесь от местной декларации, и все будет в порядке. Кроме того, я бы переместил этот код в viewDidAppear :, а не в viewDidLoad.

+0

Ну, это все еще не работает. Он либо сбой, либо я получаю нулевое значение, которое действительно странно. Хаммм, какие-нибудь идеи почему? – 0SX

+0

Ничего, наконец, понял вопрос. Симулятор действовал. благодаря – 0SX

0

С iOS5 вы можете использовать свойство в alertView, что намного проще, чем добавить textField.

Если вы хотите добавить TextField к UIAlertView, вы можете использовать это свойство (alertViewStyle) для 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); 
} 

Проверить ссылку UIAlertView: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html

Hugues

Смежные вопросы