Understanding protocol implementation with example UIAlertViewDelegate
There are two classes A and B :
@interface A : NSObject < UIAlertViewDelegate > { ... } @end
@interface B { ... }
A will use B via creating B and use it.
In B, there are chances to create UIAlertViewDialog, and do some user interactions.
I think that it i开发者_如何学运维s reasonable to implement clickedButtonAtIndex in class A, because A is declared to confirm to UIAlertViewDelegate. But the true is that we should implement in B.
- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex { //bla bla }
I really can not understand what is the true cause to confirm protocol like this case.
Thanks in advance.
When creating the UIAlertView you define what will be the delegate class:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: title
message: message
delegate: delegate
cancelButtonTitle: firstButtonName
otherButtonTitles: nil];
So if you create the UIAlertView in class B, but you want to react on the button tap in class A you need to set the delegate in the code above to class A.
精彩评论