0

こんにちは、AlertView TextField から値/テキストを取得できません。誰かが私のコードを見て、私が間違っていることを理解できるかもしれません。OK を押すたびに、ラベルは (null) 値を取得します。ここで何かが欠けているに違いありません。

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
4

2 に答える 2

0

myTextField は viewDidLoad のローカル変数ですが、クラスのメンバー変数でもあります。local 宣言を取り除けば、問題ないはずです。また、このコードを viewDidLoad ではなく viewDidAppear: に移動します。

于 2009-12-30T22:05:25.527 に答える
0

iOS5 以降、alertView でプロパティを使用できます。これは、textField を追加するより簡単な方法です。

TextField を UIAlertView に追加する場合は、UIAlertView にこのプロパティ (alertViewStyle) を使用できます。

- (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

ユーグ

于 2013-03-17T21:31:48.803 に答える