Ios 将UITextField字符串复制到UITextView

Ios 将UITextField字符串复制到UITextView,ios,uitextfield,uitextview,Ios,Uitextfield,Uitextview,如何将UITextField字符串复制到UITextView? 我想在ui按钮时执行此操作 [myUIButton addTarget:self action:@selector(touchButton) forControlEvents:UIControlEventTouchUpInside]; UITextField* textField (initialized, omit code here) [self.view addSubview:textField]; //save strin

如何将
UITextField
字符串复制到
UITextView
? 我想在
ui按钮
时执行此操作

[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?“

因此,将textView设置为ivar,您将看到它您还可以标记文本字段和文本视图并检索它们。例如:
[self.view view withtag:kTxtFieldTag]
;谢谢。成功了。我还有一个问题。每次调用该方法时,它都会创建UITextField和UITextView。你没有记性问题吗?这样似乎更好,//property UITextField*myTextField;//属性UITextView*myTextView;然后在self.myTextField=self.myTextView的方法中使用它们,这意味着它将只使用局部属性变量,而不创建新类。是的,您是对的,您应该将它们作为实例变量保留,但您曾问过“我应该如何将UITextField.text传递给UITextView?”。为了实现这一点,我就这样做了。你是对的&根据需要修改代码。祝你一切顺利!我得到了它。祝你一切顺利。