I’m reading “Programming in Objective C” by Stephen G. Kochan, and in chapter 11 he mixed with a bit about: categories, protocols, delegation, informal protocols.
Now, he just talked a bit about everyone of them and it got me more confused…I know that delegation is one of the most important subjects in obj c and that it goes along with protocols.
Please help, it’s important for me so I will not mess it up.
tnx
I’ll try and explain delegation for you. It’s really simple when you do know, but it takes time to get your head around it!
Let’s say you have two classes, a
Calculatorclass that performs calculations and aCalculatorScreenclass that is used to present the result of a calculation to a user. The Calculator class should tell the CalculatorScreen when it has finished performing a calculation so the latter can update the UI.A protocol provides a way to define a set of methods that are somehow related with a specified name. You could have a number of methods defined in a protocol called CalculatorDelegate in the Calculator class, but the method implementations are defined elsewhere.
The class that defines the protocol (in this case Calculator) can tell a delegate (an object that conforms to the protocol – in this case CalculatorScreen) to implement the method. The calculator class might finish an addition calculation and tell its delegate (the screen) to update. You get me?
Sorry, as I was writing I realized it is hard to explain and sympathized with every author that has tried!
iOS Example:
When you set up a table on the iPad’s display, you use the UITableView class. But that class doesn’t know what the title of the table is, or how many sections and rows it is to have, or what to fill it with. So it delegates that responsibility to you by defining protocols called UITableViewDataSource and UITableViewDelegate. When the UITableView needs to know some information, for example, number of rows, it calls the appropriate method on the delegate (your own class), the delegate class contains the implementation of those methods defined in the protocol.
In answer to your question, I’d drop the book for a bit and start coding what you have learned so far in a dummy app! The best way to learn is to do (for me at least).