Using delegates in iOS6, how would I conceptually design a delegate to-do list that allows me to add and edit tasks?

To be honest, the whole delegation is a little confusing to me, but I'm trying to get better.

I currently have a UITableView with a list of tasks, and I have a + button that invokes a modal view to add a new task. You can also click on each task to change the name.

How to implement this using delegation? Obviously, modal view results will change the root UITableView (add a new cell), so I think delegation is the best option. And the same for editing.

Am I doing two separate? One? Can someone conceptually show me what the delegation scheme will look like?

+4
source share
2 answers

I would call it somewhat differently / more simply: delegation is one of several ways to allow classes to communicate with each other. This is not unique to Objective-C, and it is not always the right tool for every job.

TL DR: Don't worry about it - bye!

Do not worry about which delegation is still; he likes to ask how knives are made when you just want to cut a tree. In this case, you will learn more (and faster), and the Apple UITableView docs works fine:

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/AboutTableViewsiPhone/AboutTableViewsiPhone.html

What you are trying to do is VERY structured and you will not need to invent new code - in fact, you basically cannot; Apple has already decided how UITableView works, so understanding the theory behind it will not help (for now).

So, don’t be afraid and just hit the link above - maybe some UITableView tutorials on Google - and when you encounter certain problems (and you WILL DEFINITELY) post them here on SO. :)

Later: 1 minute delegation review

.. and then one day the time will come when you need to start creating your own classes and subclasses that should talk to each other. This is when you need to know about a deletion!

Imagine you have an instance of the UIViewController class (which I will simply call "You") that shows a UITableView ("Table"). In this case, Apple designed the UITableView to use delegation so that the table can ask you a bunch of questions. The table may ask you: how many rows do I need to show? How tall should each row be? What should be the cell in row 72? You know this, but Table does not, so Table will offer you an answer through the delegation / data source methods. Your answer is answered by the answer.

Even better: classes may be forced to tell you everything! For example: “user just tapped on a cell” or “I'm going to fire myself” or “the screen just turned”. The beauty of these methods is that it allows you to respond to all critical events and prepare for them. This is the HUUUUUUGEE part of iOS programming, and you will come across it often.

This delegation in a nutshell, and there are even more amazing ways to communicate between classes - for example, blocks and even direct storage of variables in some cases.

And if you really really want to know more, although I would not recommend it, you can check out another good Apple document here:

http://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html

+2
source

Delegation is a fancy word for a simple concept: stand-in. The UITableView is configured to use a delegate (stand-in), so you don't need to subclass it every time you want to use it. Instead of subclassing and overriding (overriding) your methods, your stand will provide methods that customize the behavior of the view.

Typically, you allow your table view controller to be a table view delegate. This means that when any of his questions are asked in the table view with delegates (for example, "what is the height of this line?"), He passes this question to his stand, your controller.

Delegate methods that allow you to define its editing behavior are very clearly indicated in the UITableViewDelegate Protocol link . You may also need some methods from the UITableViewDataSource . You will definitely want to look at the Apple Table Programming Guide . Check out the sample code projects listed at the bottom of the index.

0
source

All Articles