views:

67

answers:

2

My AppDelegate_Phone class has the method -(void)doSomething. How can I call that from within a View Controller viewController?

I've tried:

[[[UIApplication sharedApplication] delegate] doSomething];

No luck.

doSomething not found in protocol

Edit. The action is being performed, but the Warning persists in XCode. What does the warning mean?

+3  A: 

The compiler is telling you exactly what is wrong (if a bit impenetrable because of the nested expressions); -delegate returns an object that responds to the UIApplicationDelegate protocol, of which your method is not a part of that protocol.

The most straightforward way to solve this is with a typecast:

[(AppDelegate_Phone *)[[UIApplication sharedApplication] delegate] doSomething];
bbum
Excellent. Thanks. +1 for compacting to one line.
Moshe
+1  A: 

You have to cast the delegate to your custom delegate class then call the doSomething. Code example is below:

MyAppDelegate *myAppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[myAppDelegate doSomething];
keno
Thank you. I did use bbum's version of the code. I prefer compact code in certain cases, over clarity.
Moshe