[英]Copy UITextField string to UITextView
如何將UITextField
字符串復制到UITextView
? 我想當UIButton
;
[myUIButton addTarget:self action:@selector(touchButton) forControlEvents:UIControlEventTouchUpInside];
UITextField* textField (initialized, omit code here)
[self.view addSubview:textField];
//save string to the property
self.textFieldString = textField.text;
//textFieldString is @property NSString* textFieldString; at the header.
UITextView* textView (initialized, omit code here)
[self.textView setEditable:false];
[self.view addSubview:self.textView];
//here i want to implement UITextField string -> UITextView display
-(void)submitButtonClicked {
//....
//Problem i am having here is that, I can not see instance variable other than @property variable. How should I pass UITextField .text to UITextView?
}
准備每個UI的標識符
[textField setTag:100];
[self.view addSubview:textField];
[textView setTag:200];
[self.view addSubview:textView];
識別每個UI元素並進行管理
-(void)submitButtonClicked {
//Problem i am having here is that,
// I can not see instance variable other than @property variable.
// How should I pass UITextField .text to UITextView?
UITextField *myTextField=(UITextField*)[self.view viewWithTag:100];
UITextView *myTextView=(UITextView*)[self.view viewWithTag:200];
myTextView.text=myTextField.text;
}
在按鈕的操作中使用:
textView.text=textField.text;
在@interface和@end之間聲明變量,並在viewDidLoad中初始化它們。 這樣,您就可以在按鈕操作中使用它們。
@interface ViewController ()
{
UITextView *textView;
UITextField *textField;
}
@implementation ViewController
-(void) viewDidLoad
{
// Do the following
// Initialize both textView and textField
// Set their frames
// Add both of them as a subview to your view
}
@end
現在,您可以在按鈕的操作中訪問它們兩者。 希望這可以幫助。
如果要以編程方式創建UITextView,則創建一個屬性變量並將其合成。 您可以使用綜合名稱訪問相同的名稱,並在UIButton操作方法中獲取文本並將其設置為UITextView。
在.m文件中,您可以將UITextView聲明為
@interface classname () {
UITextView *textView
}
或.h文件中
@property (nonatomic, strong) UITextView *textView;
正如您所描述的問題,“我在這里遇到的問題是,我看不到@property變量以外的實例變量。 我應該如何將UITextField .text傳遞給UITextView?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.