开发者

Understanding protocol implementation with example UIAlertViewDelegate

开发者 https://www.devze.com 2023-01-04 13:45 出处:网络
There are two classes A and B : @interface A : NSObject < UIAlertViewDelegate > {... } @end @interface B {... }

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消