I’m confused – I cannot understand what is the delegate is for?
The Application Delegate which is created by default is understandable, but in some cases I’ve seen something like this:
@interface MyClass : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
BOOL pageControlUsed;
}
//...
@end
What is the <UIScrollViewDelegate> for?
How does it work and why is it used?
<UIScrollViewDelegate>is saying that the class conforms to theUIScrollViewDelegateprotocol.What this really means is that the class must implement all of the required methods defined within the
UIScrollViewDelegateprotocol. Simple as that.You can conform your class to multiple protocols if you like:
The purpose of conforming a class to a protocol is to a) declare the type as a conformant of the protocol, so you can now categorize this type under
id <SomeProtocol>, which is better for delegate objects that objects of this class may belong to, and b) It tells the compiler to not warn you that the implemented methods are not declared in the header file, because your class conforms to the protocol.Here’s an example:
Printable.h
Document.h
Document.m
You could then have multiple objects that conform to the
Printableprotocol, which could then be used as an instance variable in, say, aPrintJobobject: