![](/img/trans.png)
[英]IOS Using method from ViewController.m in AppDelegate.m?
[英]How to call function in ViewController.m from AppDelegate?
- (void)applicationDidBecomeActive:(UIApplication *)application {
UIViewController* root = _window.rootViewController;
UINavigationController* navController = (UINavigationController*)root;
UIViewController mycontroller = (UIViewController )[[navController viewControllers] objectAtIndex:0];
[mycontroller serverSync];
}
我使用此代碼,但出現錯誤:
ld:架構x86_64 clang的110個重復符號:錯誤:鏈接器命令失敗,退出代碼為1(使用-v查看調用)
怎么修?
110 duplicate symbols
表示,與嘗試從應用程序委托中調用視圖控制器的serverSync
函數相比,您遇到的問題要serverSync
。
與其在您的應用程序委托中進行serverSync
將其放入視圖控制器的viewDidLoad
方法中。
更好的是,創建一個執行serverSync
的單例對象,並且視圖控制器可以從那里訪問和使用服務器數據。
您可以為此使用NSNotificationCenter。 這是例子。
在您的AppDelgate.m中
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"iOStpoint.wordpress.com"
object:self];
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
在您的ViewController.m中
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"iOStpoint.wordpress.com"
object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"iOStpoint.wordpress.com"])
NSLog (@"Successfully received the test notification!");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.