views:

335

answers:

1

I have a UITextView containing a document. If the user touches the document, the insertion point (selectedRange property) is set appropriately and the UITextView becomes the first responder (keyboard appears). YAY!

How can I do the same thing programmatically? Let's say I have a button titled "Edit at character 1,000". I want that to set selectedRange to [1000, 0] and then make the textview become the first responder.

Problems...

  1. textview.selectedRange setter only seems to have an effect when called from viewDidAppear.

  2. [textview becomeFirstResponder] sets the insertion point to the end of the document.

So, the best I can do is first becomeFirstResponder and then set selectedRange. The user sees the view scroll to the bottom of the document and then back up to the desired insertion point. Kinda ugly.

Should I try to hide the ugliness by hacking 'scrollEnabled' and 'editable' flags during the transition? Or is there a better way to do this?

A: 

You may be stuck doing a hack. How about this one:

Hide the view when you set the selected range (perhaps by putting another view of the same size over the UITextView) to hide the ugly scrolling. Then when the range is selected set unhide the view, some time after viewDidAppear.

Chris Garrett