Don't Understand Links back to 'delegate' in Figure 5.5


In chapter 1 we linked our target/action from the 'Show Question" and “Show Answer” UITextFields to the ‘showAnswer’ and ‘showQuestion’ methods (Fig 1.16) we had set up in QuizviewController.m and declared as IBActions in QuizViewController.h

In Chapter 5 we established no such IBAction in the WhereamiViewcontroller header or implementation file, yet somehow we link back to a ‘delegate’. I followed the instructions in figure 5.4 but I do not know:

  • where the ‘delegate’ IBAction came from,
  • why we didn’t declare it in the header file
  • why there are no methods associated with the ‘delegate’ method in the implementation file; nor
  • how it is that we can link back two different obects in the xib to just one method?

I wish the text here made it a bit clearer what we were supposed to do here and why.

Still confused … but plowing forward anyway.



Hi Dale,

OK. First of all let me tell you that the book describes on page 87f. what delegation means. I recommend you reading it, if you didn’t really get the idea of delegation.

But for now let me try to answer your questions:

I think your question is, why there isn’t any function looking like this:

  • (IBAction)doSomething;
    in the header oder implementation file, right ?

The answer is that there is no need to declare such methods. When you have a target-action-pair, your controller can be sent any message. To tell XCode which messages should be triggered, when something (a tap for example) occurs, you link the message by control-dragging from the object in the interface builder to the File’s owner and selecting a message. To tell XCode which methods are reserved for these events, you take IBAction as the return value.

With delegation you got a different situation. It’s like a contract. The object that wants its events to be delegated has a protocol which lists the messages it may send. The delegate of it should conform to that protocol (meaning that the delegate implements all the required methods of the protocol). Now the contract: The object says: “I will only send you messages in that protocol” and the delegate says: “And I will implement all those methods, so the program doesn’t crash”.

We say that the Class conforms to the delegate-protocol. That’s it.

All methods in the protocol are associated with the delegated object(s).

I’ll give you another metaphor. There are two guys standing in front of an electric fence. Now one starts peeing against the fence and gets a shock. The other one stands there without getting a shock. If now the other guy starts peeing against the same fence he also gets a shock. The “delgation” of their peeing is completely independent from the peeing of the other guy.
I hope that example wasn’t too disgusting - but it was the first thing that came to my head :slight_smile:




That was REALLY helpful. You know, I kinda knew all that in the abstract but never put it all together. I’m stil somewhat confused about protocols and how they work exactly but I will do as the book advised in the intro - keep typing away like a trained monkey - and hope that this idea becomes clearer as I work along. So far this is the only stumbling block I’ve had in the book.

I think part of the problem is that I read through and did the protocol-related exercises late at night after my screaming child had been FINALLY put to bed. I was a bit frazelled. I’ll definately go back and re-read all this stuff now that you’ve given me a solid grounding to go forward.

Thanks so much!! I really appreciate people willing to answer questions for those of us who came late to this book! :slight_smile:



OK, its two weeks later and I’ve now completely re-read chapter 4 on delegation and protocols and I think I get this. Joerg’s answers were REALLY helpful in helping me get this.

Here are my own answers to my questions.

  • There is no delegate IBAction. The classes that has delegated to the controller will handle the messages coming from the ‘map view’ and the ‘text field’. The WhereamiViewController will not.
  • We didn’t declare the actions in the controller’s header becauase the controller implementation file won’t take action on the mesesages, the delegating class will.
  • Answered in the answer above.
  • There isn’t just one method. There is a link to the delegate which in turn connects back to the two applicable methods in the delegating class.

I hope I’m right here.

Pressing on.