12

In a class method method I declare the following:

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Hello" 
                                                   message:@"World"
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel" 
                                         otherButtonTitles:@"Done", nil];

Now even though the containing method is a class method, the compiler does not complain about the delegate:self. Now to be the delegate I have to implement the method in UIAlertViewDelegate protocol.

Does it now matter, if the method is a class method or not, in order to be called when the user interacts with my AlertView?

I am asking this question, because my class does not have any instance variables so I would prefer it to contain class methods only.

Besi
  • 22,579
  • 24
  • 131
  • 223

3 Answers3

14

I would like to share some of my findings:

  1. It does not matter whether I declare the delegate like this delegate:self or delegate:[MyClass class]

  2. However, it does matter how I declare the method:

    This works

    +(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        // Some Actions here
    }
    

    Although the following "syntax" is the declaration in the UIAlertViewDelegate protocol, it does not work:

    // Mind the `-` sign at the start of the line.
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        // Some Actions here
    }
    
Besi
  • 22,579
  • 24
  • 131
  • 223
  • 1
    Yes, `self` and `[MyClass class]` are equivalent within a "MyClass" method. If you want your class itself to be the delegate, then you have to declare this as a class method. The documentation is describing how to implement the instance method, which I think is pretty much universally "how it's done". In the end what really matters is that whatever object you provide as the delegate responds to the "alertView:clickedButtonAtIndex:" selector. So, yes, the answer is still that you *can* do it. Whether you should is different. – huskerchad Jan 16 '12 at 18:17
  • Wow! Not in a million years would I have guess that turning the delegate method into a class method would be the way to go. Thank you, thank you! I have lost 5+ hours trying to solve a similar problem. ... didn't need NSCondition or performSelector or anything! – JohnQ May 22 '13 at 02:39
  • I'm using a class method as my `MFMailComposeViewControllerDelegate`, and I get this warning: `Incompatible pointer types assigning to 'id _Nullable' from 'Class _Nullable'` My @interface has ``, but how do I do that for a class method and avoid the warning? – Jeff Apr 29 '20 at 04:09
  • @Jeff. My answer is 8 years old so I'm a bit out of practise. I suggest you open a new question and maybe reference this post in it. I'm sorry to not being able to offer you a better answer. – Besi Apr 29 '20 at 09:08
  • @Besi. Thank you. I've found a reasonable workaround (it gets rid of the warning). I add `(id)` thusly, `controller.mailComposeDelegate = (id)[self class];` – Jeff Apr 29 '20 at 09:31
11

First, I assume by "static" methods, you mean class methods (declared with + instead of -).

"The compiler does not complain" about Objective-C doesn't always tell you much. All it really means is that you have convinced the compiler that everything should (or at least could) work at runtime.

In the end, if the object you provide as a delegate responds to the right messages, the compiler won't care that it's a class (and neither will the runtime). In this case, you are providing self from a class method. It is as though you typed [MyClass class].

I think it's probably questionable whether this is what you should do--but that's probably outside the bounds of your question.

huskerchad
  • 1,026
  • 8
  • 9
1

Yes you can !!! A class is also an object.

Vincent Zgueb
  • 1,491
  • 11
  • 11