将UITextField字符串复制到UITextView

时间:2014-02-28 10:45:52

标签: ios uitextfield 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?
}

3 个答案:

答案 0 :(得分:0)

为每个用户界面准备标识符

[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;

}

答案 1 :(得分:0)

在按钮的操作中使用:

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

现在,您可以在按钮的操作中访问它们。 希望这会有所帮助。

答案 2 :(得分:0)

如果以编程方式创建UITextView,则创建一个属性变量并合成它。您可以使用合成名称访问相同的名称,并在UIButton操作方法中获取文本并将其设置为UITextView。

在.m文件中,您可以将UITextView声明为

@interface classname () {
    UITextView *textView
}

或.h文件

@property (nonatomic, strong) UITextView *textView;

正如您所描述的问题'我遇到的问题是,我看不到@property变量以外的实例变量。我应该如何将UITextField .text传递给UITextView?'