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 t开发者_高级运维he Warning persists in XCode. What does the warning mean?
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];
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];
精彩评论