tags:

views:

14

answers:

1

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 is 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.

A: 

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.

Shingoo
Thanks for your answer, that clears my confusion.
Forrest
Would be great if you could accept the answer (by clicking on the checkmark) if it was the info you were looking for ;)
Shingoo