URL I am adding text inside the uitextview using uipickerview.
Currently i am adding the text as below ( did select row method )
self.myTextView.text = [NSString stringWithFormat:@"%@ %@", self.myTextView.text,
[myTierThreeData objectAtIndex:row]];
This works fine but i wanted to insert text where the cursor is so i did the below
-(void) pickerViewShown:(id)sender{
myCursorPosition = [self.myTextView selectedRange];
}
When ever the pickerview is shown, taken the cursor position in the member variable.
- (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];
}
the above code works for the first time but when try to add next text , it appends the text at the end.
please let me know what is wrong with the above code
Probably setting the text for the text field resets the cursor position to the end, so the only chance you have is to calculate the new cursor position yourself:
(like you do now) in myCursorPosition.
add the text length to myCursorPosition, and use that value for
inserting the next text.