I noticed that in creating protocols we can define methods as optional in the iPhone SDK, for example UIActionSheetDelegate protocol.
How can I define a protocol of my own, and set as optional of few of the methods?
Home / DeveloperSection / Forums / How to create optional methods in a protocol
I noticed that in creating protocols we can define methods as optional in the iPhone SDK, for example UIActionSheetDelegate protocol.
How can I define a protocol of my own, and set as optional of few of the methods?
Anonymous User
27-Sep-2015As an example, the contact chart view might test for the segment title method like this:
NSString *contactTitle;
if ([self.dataSource respondsToSelector:@selector(titleForContactAtIndex:)]) {
contactTitle = [self.dataSource titleForContactAtIndex:index];
}
The respondsToSelector: method uses a selector, which refers to the identifier for a method after compilation. You can provide the correct identifier by using the @selector() directive and specifying the name of the method.
If the data source in this example implements the method, the title is used; otherwise, the title remains nil.