Passing data from ViewController to AppDelegate
How can I execute a method in AppDelegate from MainViewController.
[super performSelector:@selector(doSomething)];
and the method doSomething is defined 开发者_Go百科in the AppDelegate.m
-(void) doSomething {
NSLog(@"Method Executed");
}
Assuming you want it for iOS, a simple call :
[[[UIApplication sharedApplication] delegate] doSomething];
should do it. On Mac Os you use NSApplication
instead.
As your app delegate isn't (is it ?) the superclass of your main view controller, you can't do it as in your question.
But also, a better way should be :
[[UIApplication sharedApplication] sendAction:@selector(doSomething)
to:nil
from:self];
This way (specifying nil
as the reciever), the action message will go through the responder chain, and will finally be handled by your appliation delegate if none of the responder objects can deal with it.Also, this way prevent you from referencing the app delegate directly in a custom view controller.
Here is the doc for more information about this method.
精彩评论