【发布时间】:2012-04-30 08:17:21
【问题描述】:
URL 我正在使用 uipickerview 在 uitextview 中添加文本。
目前我正在添加如下文本(选择行方法)
self.myTextView.text = [NSString stringWithFormat:@"%@ %@", self.myTextView.text,
[myTierThreeData objectAtIndex:row]];
这很好,但我想在光标所在的位置插入文本,所以我做了以下操作
-(void) pickerViewShown:(id)sender{
myCursorPosition = [self.myTextView selectedRange];
}
当显示pickerview时,在成员变量中获取光标位置。
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *contentsToAdd = [myData objectAtIndex:row];
NSMutableString *tfContent = [[NSMutableString alloc] initWithString:[self.myTextView text]];
// add content where the cursor was
NSString *contentsToAddPadded = [NSString stringWithFormat:@"%@ ", contentsToAdd];
[tfContent insertString:contentsToAddPadded atIndex:myCursorPosition.location];
[self.myTextView setText:tfContent];
[tfContent release];
myCursorPosition = [self.myTextView selectedRange];
}
上面的代码第一次工作,但是当尝试添加下一个文本时,它会在末尾附加文本。
请告诉我上面的代码有什么问题
【问题讨论】:
-
第一件事是尝试 nslogging myCursorPosition.location in didSelectRow
-
第一次 nslogging 时,当我将光标放在两个单词之间时,它会正确打印,它会将光标位置显示为总字符串长度。我能做什么
标签: iphone ios ios4 uitextview uipickerview