您可以添加文本字段到一個數組,在一個循環中修改其屬性:
NSArray *textViews= [NSArray arrayWithObjects:textView1, textView2, textView3, nil];
for(UITextView *txtView in textViews)
txtView.text = [txtView.text stringByAppendingString:@"myAsciiString"];
[textViews release];
編輯
如果你想知道哪個textView發起了動作(假設你連接了多個textViews相同的IBAction爲),你既可以設定和查看視圖的標籤,或者你可以用一個實例進行比較:
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"];
}
如果你想分配一個值到活動文本視圖,你可以遍歷你的子視圖,如果它有第一響應者,設置值:
- (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被點擊進行編輯,並使用上面列出的IBAction插入文本「僅在活動文本視圖中」,而不是全部插入。再次感謝! – Dan 2012-01-05 02:56:53
已更新,以反映您的評論。 – Jeremy 2012-01-05 03:14:43
此方法(選項1)仍然評估爲true,並同時將我的文本插入到所有文本視圖中。我已將操作連接到IB中的一個按鈕,該按鈕應插入基於活動文本視圖的文本。我很感激你的幫助,但我必須在你的指導中遺漏一些東西。 – Dan 2012-01-05 03:32:18