2013-08-02 93 views
0

在我的viewDidLoad我創建一個文本框象下面這樣:以編程方式添加textField並使用另一種方法從中讀取?

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爲方法的文本框的文本..

如何將我從IBAction爲訪問內文本框的文本?

回答

3

通過在界面添加伊娃保持你的UITextField對象的引用:

@interface MyViewController : UIViewController 
{ 
UITextField *textField; 
} 

,並在.m文件添加方法:

- (IBAction)readTextField: (id)sender 
{ 
NSLog(@"%@", textfield.text); 
} 
+0

哦,我已經錯過了界面中的參考 - 謝謝你很多人生病接受盡快! :) – Alosyius

0

你可以保持一個參考到你UITextField對象通過在界面中添加一個ivar或創建textField的屬性。

@interface MyViewController : UIViewController 
{ 
    UITextField *textField; 
} 

或創建一個屬性

@property(nonatomic, retian) UITextField *textField; 

和合成它

@synthesize textField; 

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

在這兩種情況下,它會工作,更主要的是讓你文本框全球使其可以從任何方法訪問。

相關問題