[英]Issue - Insert text works only for first time , the second time it appends to the end of the text
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];
}
上面的代码是第一次工作,但是当尝试添加下一个文本时,它将在文本末尾追加。
请让我知道以上代码有什么问题
可能为文本字段设置文本会将光标位置重置为末尾 ,因此唯一的机会是自己计算新的光标位置 :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.