0

I'm tying to call didDismissWithButtonIndex on UITextView Class ,but it not called. I also implement UIAlertViewDelegate on MyViewcontroller.h file and [alert setDelegate:self] to method.

So is that possible to call UIAlertView Delegate method in UITextView Class ??

+ (void)deleteTextr:(UITapGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        UIAlertView *alert  = [[UIAlertView alloc]initWithTitle:@"" message:@"Delete Text !!!!" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"No", nil];
        [alert setDelegate:self];
        [alert show];
    }
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1)
    {
        [self removeFromSuperview];
    }
}
Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
iBhavin
  • 1,261
  • 15
  • 30

2 Answers2

2

deleteTextr: is a class method. In the context of a class method, self is just a reference to the class. To use the UIAlertViewDelegate protocol, you need assign an instance of a class to the UIAlertView instance's delegate property, which can only be done within an instance method.

Read this to get a better grasp of the aforementioned concept.

Community
  • 1
  • 1
Vatsal Manot
  • 17,695
  • 9
  • 44
  • 80
  • It's perfectly valid to use `self` as a class reference as the delegate. It's not typical but it does work. Of course the delegate method needs to be a class method for it to work. – rmaddy Jul 10 '14 at 05:51
2

just change delegate method to class method. like this:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;

// - change to +

+ (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
simalone
  • 2,768
  • 1
  • 15
  • 20