What's the proper way to setup different objects as delegates using Interface Builder?
Posted
by eagle
on Stack Overflow
See other posts from Stack Overflow
or by eagle
Published on 2010-03-20T02:58:50Z
Indexed on
2010/03/20
3:01 UTC
Read the original article
Hit count: 532
Let's say I create a new project. I now add two text fields to the view controller in Interface Builder. I want to respond to delegate events that the text fields create, however, I don't want to have the main view controller to act as the delegate for both text fields. Ideally I want a separate file for each text field that acts as the delegate. Each of these objects also needs to be able to interact with the main view controller.
My question is how I would set this up and link everything correctly?
I tried creating a new class that inherits from NSObject and implements UITextFieldDelegate. I then added an instance variable called "viewController" of the same type of my view controller and marked it with IBOutlet (this required me to add #import "myViewcontroller.h").
I then went to Interface Builder and opened up my view controller which contains the two edit boxes. I added an NSObject to the form and changed it's type to be of the new class I created. I set its viewController property to the File's Owner, and set one of the textbox's delegate properties to point to this new object I created.
Now when I run the program, it crashes when I touch the text box. It gives the error EXC_BAD_ACCESS. I'm guessing I didn't link stuff correctly in IB.
Some things I'm not sure about which might be the problem:
Does IB automatically know to create an instance of the class just by placing the NSObject in the ViewController? Can it properly assign the viewController property to an instance of itself even though it is creating itself at the same time?
© Stack Overflow or respective owner