开发者

Make the App Delegate perform an action?

开发者 https://www.devze.com 2023-01-04 13:17 出处:网络
My AppDelegate_Phone class has the method -(void)doSomething. How can I call that from within a View Controller viewController?

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号