[英]Passing data from ViewController to AppDelegate
如何從 MainViewController 執行 AppDelegate 中的方法。
[super performSelector:@selector(doSomething)];
並且方法 doSomething 在 AppDelegate.m 中定義
-(void) doSomething {
NSLog(@"Method Executed");
}
假設你想要它用於 iOS,一個簡單的調用:
[[[UIApplication sharedApplication] delegate] doSomething];
應該這樣做。 在 Mac Os 上,您使用NSApplication
代替。 由於您的應用程序委托不是(是嗎?)您的主視圖 controller 的超類,因此您不能像在您的問題中那樣做。
而且,更好的方法應該是:
[[UIApplication sharedApplication] sendAction:@selector(doSomething)
to:nil
from:self];
這樣(指定nil
作為接收者),動作消息將通過響應者鏈 go,如果沒有響應者對象可以處理它,最終將由您的應用程序委托處理。此外,這種方式可以防止您引用應用程序直接在自定義視圖 controller 中委托。
這是有關此方法的更多信息的文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.