UITextField SigAbrts on selectedTextRange

898 Views Asked by At

I'm trying to find and delete the selected text in a text field, but it SIGABRTs every time. I keep getting an NSInvalidArgumentException when I try to retrieve the selectedTextRange. In fact I tested it and every function having to do with the selected text seems to break the app.

[mTextField selectedTextRange];

Even that line of code itself is an unrecognized selector for some reason. I've double checked that I'm not using garbage data or anything, I'm able to get the text first and then getting the selected text afterwards breaks it.

3

There are 3 best solutions below

1
On BEST ANSWER

selectedTextRange can only use on iOS5+.

1
On

Since you tagged your question with nstextfield, I assume you're using an NSTextField (Mac OS X) rather than a UITextField (iOS). The problem is NSTextField doesn't support the selectedTextRange message. (UITextField does support it.) I don't see any way to get the selected range from an NSTextField.

If you switch to using NSTextView, you can send it the selectedRanges message.

0
On

I seriously dont think that you can achieve that by using UITextField.

However I think u can sort of do that by implementing UITextViewDelegate. You can use selectedRange property in this method.

- (void)textViewDidChangeSelection:(UITextView *)textView

By the way your question might have something to do with this question:

Can I select a specific block of text in a UITextField?

Do check it out..