繁体   English   中英

问题-插入文字仅在第一次使用时有效,第二次追加到文字末尾

[英]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];
}

上面的代码是第一次工作,但是当尝试添加下一个文本时,它将在文本末尾追加。

请让我知道以上代码有什么问题

可能为文本字段设置文本会将光标位置重置为末尾 ,因此唯一的机会是自己计算新的光标位置

  • 启动选择器视图时,请记住myCursorPosition中的原始光标位置(就像现在一样)。
  • 每次添加文本时,请将文本长度添加到myCursorPosition ,并使用该值插入下一个文本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM