0

在我的 viewDidLoad 中,我正在创建一个如下所示的文本字段:

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f,
                                                                       6.0f,
                                                                       toolBar.bounds.size.width - 20.0f - 68.0f,
                                                                       30.0f)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[toolBar addSubview:textField];

但我需要从 IBAction 方法的 textField 中读取文本。

我将如何从该 IBAction 访问 textField 中的文本?

4

2 回答 2

3

通过在界面中添加 ivar 来保持对 UITextField 对象的引用:

@interface MyViewController : UIViewController
{
UITextField *textField;
}

并在 .m 文件中添加您的方法:

- (IBAction)readTextField: (id)sender
{
NSLog(@"%@", textfield.text);
}
于 2013-08-02T15:58:15.037 回答
0

You can Keep a reference to your UITextField object by adding an ivar in the interface or create a property of your textField.

@interface MyViewController : UIViewController
{
   UITextField *textField;
}

or create a property

@property(nonatomic, retian) UITextField *textField;

and synthesize it

@synthesize textField;

- (IBAction)GetTextFiledValue: (id)sender
  {
      NSLog(@"Your TextField value is : %@", [textfield text]);
  }

In the both case it'll work, but the main thing is that make you textField global so that it could be accessable from any method.

于 2013-08-02T19:31:14.980 回答