开发者

Passing data from ViewController to AppDelegate

开发者 https://www.devze.com 2023-03-18 05:16 出处:网络
How can I execute a method in AppDelegate from MainViewController. [super performSelector:@selector(doSomething)];

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.

0

精彩评论

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