PROTOCOLS

In Objective-C, a protocol declares a programmatic interface that any class can choose to implement. A protocol declares a set of methods, and an adopting class may choose to implement one or more of its declared methods. The class that defines the protocol is expected to call the methods in the protocols that are implemented by the adopting class.

The easiest way to understand protocols is to examine the UlAlertView class. As you have experienced in the various chapters in this book, you can simply use the UlAlertView class by creating an instance of it and then calling its show method:

UlAlertView *alert = [[UlAlertView alloc]
                          initWithTitle:@“Hello”
                                message:@“This is an alert view”
                               delegate:self
                      cancelButtonTitle:@“OK”
                      otherButtonTitles:nil];
[alert show];

The preceding code displays an alert view with one button — OK. Tapping the OK button automatically dismisses the alert view. If you want to display additional buttons, you can set the otherButtonTitles: parameter like this:

UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@“Hello”
                                message:@“This is an alert view”
                               delegate:self
                      cancelButtonTitle:@“OK”
                      otherButtonTitles:@“Option 1”, @“Option 2”, nil];
[alert show];

The alert view now displays three buttons — OK, Option 1, and Option 2. How do you know which button was tapped by the user? You can determine that by handling the relevant method(s) that will be fired by the alert view when the buttons are clicked. This set of methods is defined by the UIAlertViewDelegate protocol, which defines the following methods:

  • alertView:clickedButtonAtIndex:
  • willPresentAlertView:
  • didPresentAlertView:
  • alertView:willDismissWithButtonIndex:
  • alertView:didDismissWithButtonIndex:
  • alertViewCancel:

If you want to implement any of the methods in the UIAlertViewDelegate protocol, you need to ensure that your class, in this case the View Controller, conforms to this protocol. A class conforms to a protocol using angle brackets (<>), like this:

@interface ObjCTestViewController : UIViewController
    <UIAlertViewDelegate> {  //---this class conforms to the
                        // UIAlertViewDelegate protocol---
}

@end

image NOTE To conform to more than one delegate, separate the protocols with commas, such as <UIAlertViewDelegate, UITableViewDataSource>.

After the class conforms to a protocol, you can implement the method in your class:

- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {

    NSLog([NSString stringWithFormat:@“%d”, buttonIndex]);

}

Delegate

In Objective-C, a delegate is just an object that has been assigned by another object as the object responsible for handling events. Consider the case of the UlAlertView example shown previously:

UlAlertView *alert = [[UlAlertView alloc]
                          initWithTitle:@“Hello”
                                message:@“This is an alert view”
                               delegate: self
                      cancelButtonTitle:@“OK”
                      otherButtonTitles:@“Option 1”, @“Option 2”, nil];
[alert show];

The initializer of the UlAlertView class includes a parameter called the delegate. Setting this parameter to self means that the current object is responsible for handling all the events fired by this instance of the UlAlertView class. If you don't need to handle events fired by this instance, you can simply set it to nil:

UlAlertView *alert = [[UlAlertView alloc]
                          initWithTitle:@“Hello”
                                message:@“This is an alert view”
                               delegate: nil
                      cancelButtonTitle:@“OK”
                      otherButtonTitles:@“Option 1”, @“Option 2”, nil];
[alert show];

If you have multiple buttons on the alert view and want to know which button was tapped, you need to handle the method declared in the UlAlertViewDelegate protocol. You can either implement the method in the same class in which the UlAlertView class was instantiated (as shown in the previous section), or create a new class to implement the method, like this:

//---SomeClass.m---
@implementation SomeClass

- (void) alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {

    NSLog([NSString stringWithFormat:@“%d”, buttonIndex]);

}
@end

To ensure that the alert view knows where to look for the method, create an instance of SomeClass and then set it as the delegate:

SomeClass *myDelegate = [[SomeClass alloc] init];

UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@“Hello”
                                message:@“This is an alert view”
                       delegate:myDelegate;
                      cancelButtonTitle:@“OK”
                      otherButtonTitles:@“Option 1”, @“Option 2”, nil]
[alert show];
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.192.79