views:

526

answers:

2

How do you connect the "delegate" outlet of a UITextView to a class that implements UITextViewDelegate protocol?

I can't seem to find an example in the docs

The weird thing is the UITextView's "delegate" outlet has that drag 'n drop interface thingy, like you can wire it up to another widget but of course, I don't want to wire it up to a widget, I want to wire it up to an existing class.

+1  A: 

If you mean "how do I make an object that is created with code the delegate for my text view", just set the delegate property (this is what dragging in Interface Builder will do for you):

textView.delegate = yourDelegateObject;
Rhult
Ok, but, have you any idea why the outlet _looks like_ it can be dragged onto a widget?You can't drag the outlet onto a piece of code, so why is it designed that way?
bobobobo
Rhult
Sweet. This way works.
bobobobo
+2  A: 

To wire up a delegate in Interface Builder:

(1) Drag an appropriate controller into the nibs main window and set the class of the controller to the class of your delegate. For example, if you have an NSObject subclass called "MyDelegateClass", drag an Object controller over and set it's class to "MyDelegateClass".

(2) In the connections inspector for the UITextView, control-click on delegate and connect that to the controller created in step (1).

That's it.

TechZen
Seems reasonable enough, but now when I run the application, the application crashes without warning whenever I touch the textView..
bobobobo
Yeah. And the application WORKS when I use the `textView.delegate = yourDelegateObject ;` method that Rhult talks about.
bobobobo
Rhult's method is the programatic method. It is the one I use most of the time because its hard to find mistakes in IB. I thought you wanted to know how to set it up in interface builder and this is the correct method for doing so. A crash suggest you still don't have the delegate assigned properly. Remember that you have to fulfill two protocols for tables, a delegate and a datasource. The same class/instance can implement both but you have to wire the tables `delegate` and `datasource` outlets in IB.
TechZen
Sorry,forgot this was the post about a UITextView and its delegate not a table. A textview just has the one delegate.
TechZen
Yeah, :). I would have picked this answer, but the problem was I couldn't get it to work.I created an instance of the MyDelegate class in the xib (1) and connected up the "delegate" outlet of the UITextView to the MyDelegate instance. The program would crash immediately after touching the textView.This is the "right" answer to the question however.
bobobobo