Objective c 能否通过IB为UITextView分配变量名?
我正在开发一个应用程序,它有多个UITextView,带有自定义键盘和一些自定义菜单选项,可以插入预定义的文本。有没有办法为UITextView使用变量 下面的代码非常有用,但我需要为不止一个UITextView使用自定义键盘/按钮Objective c 能否通过IB为UITextView分配变量名?,objective-c,ios,uitextview,Objective C,Ios,Uitextview,我正在开发一个应用程序,它有多个UITextView,带有自定义键盘和一些自定义菜单选项,可以插入预定义的文本。有没有办法为UITextView使用变量 下面的代码非常有用,但我需要为不止一个UITextView使用自定义键盘/按钮 我还将有textView2、textView3等。您可以将文本字段添加到数组中,并在循环中修改其属性: NSArray *textViews= [NSArray arrayWithObjects:textView1, textView2, textView3, ni
我还将有textView2、textView3等。您可以将文本字段添加到数组中,并在循环中修改其属性:
NSArray *textViews= [NSArray arrayWithObjects:textView1, textView2, textView3, nil];
for(UITextView *txtView in textViews)
txtView.text = [txtView.text stringByAppendingString:@"myAsciiString"];
[textViews release];
编辑
如果您希望知道哪个textView启动了该操作(假设您将多个textView连接到同一iAction),则可以设置并检查视图的标记,或者与实例进行比较:
textView1.tag = 0;
textView2.tag = 1;
textView3.tag = 2;
//etc.
- (IBAction)someTextViewAction:(id)sender {
//Option 1
if (sender.tag == 0)
textView1.text = [textView1.text stringByAppendingString:@"myAsciiString"];
else if(sender.tag == 1)
textView2.text = [textView2.text stringByAppendingString:@"myAsciiString"];
else if(sender.tag == 2)
textView3.text = [textView3.text stringByAppendingString:@"myAsciiString"];
//Option 2
if ([sender isEqual:textView1])
textView1.text = [textView1.text stringByAppendingString:@"myAsciiString"];
else if ([sender isEqual:textView2])
textView2.text = [textView2.text stringByAppendingString:@"myAsciiString"];
else if ([sender isEqual:textView3])
textView3.text = [textView3.text stringByAppendingString:@"myAsciiString"];
}
如果希望为活动文本视图指定一个值,可以在子视图中迭代,如果它有first responder,则设置该值:
- (IBAction)someButtonAction:(id)sender {
for (UIView *view in self.subviews)
{
if ([view isKindOfClass:[UITextView class]])
{
if ([view isFirstResponder])
{
view.text = [view.text stringByAppendingString:@"myAsciiString"];
}
}
}
}
感谢您的快速回复,但是这将同时为所有视图添加我的文本值。也许我应该把我的问题换个说法。我希望能够知道哪个UITextView被点击进行编辑,并使用上面列出的iAction只在活动文本视图中插入文本,而不是在所有视图中插入文本。再次感谢!更新以反映您的评论。此方法选项1的计算结果仍然为true,并同时将我的文本插入所有文本视图。我将该操作连接到IB中的一个按钮,该按钮应根据活动的textview插入文本。我很感谢你的帮助,但我一定是在你的说明中遗漏了什么。更新以反映你的评论。谢谢你的帮助杰里米,我正在检查第一响应者,我看到了你的最新回复。我想这会让我暂时继续前进。谢谢你的快速回复!!!
- (IBAction)someButtonAction:(id)sender {
for (UIView *view in self.subviews)
{
if ([view isKindOfClass:[UITextView class]])
{
if ([view isFirstResponder])
{
view.text = [view.text stringByAppendingString:@"myAsciiString"];
}
}
}
}