Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Rashii Henry
Rashii Henry
16,433 Points

Using Delegates

How do i know when i need to make a class a delegate?

Could someone give me possible scenarios that delegate will be useful in?

What are delegates important and how much difference do they make?

2 Answers

Delegation is a design pattern that is worth understanding if you want to get proficient with developing for Apple platforms. And there are numerous excellent articles on it on the 'net. In essence, a delegate performs tasks that an object is not meant to perform.

Here is a real-life example. When you order a peppermint no-whip single-shot mocha from a Starbucks cashier, it does not make sense for the cashier to make the mocha for you (unless they are terribly understaffed). What usually happens is that the cashier delegates the task of making the mocha to another barista who is in charge of handling that task instead. In this scenario, you don't have to set the cashier's delegate. The barista has been assigned as the delegate of the cashier by the shift manager at 5am.

Back to iOS and using Stone's example: when the search bar cancel button is clicked, whatever needs to be done is usually not the search bar's business. So you assign the search bar a delegate (can be your current view controller, i.e. self, or it can be any other object of your choice) to perform those tasks.

One more iOS example since you asked about tableview. In the UITableViewDelegate protocol, one of the methods is tableview:didSelectRowAtIndexPath:. Same as before, whatever should transpire after a row is selected is none of the table's business. The table's job is to lay things out, not to move data around or dance. So you tell the table to delegate those tasks to someone else, which could be your_self_.

Stone Preston
Stone Preston
42,016 Points

for example, if you want to add a search bar to a view, you wold have to implement that viewcontroller as a delegate which means you will implement the delegate methods, or follow a certain protocol. so in your views .h you would have

@interface SomeViewController : UIViewController <UISearchBarDelegate>

and then somewhere in your .m for that view you would have to explicitly set the viewcontroller as the delegate

self.searchBar.delegate = self;

After you have set the view controller as the delegate, you can implement delegate methods of the search bar like

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

     [searchBar resignFirstResponder];

}
Rashii Henry
Rashii Henry
16,433 Points

so is a delegate just used when you want to communicate a control with the view controller?

is this the same for a tableview?