简体   繁体   中英

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

I use this code, but get error:

ld: 110 duplicate symbols for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

How to fix?

110 duplicate symbols means you have a lot more problems than trying to call your view controller's serverSync function from your app delegate.

Instead of doing serverSync within your app delegate, put it in your view controller's viewDidLoad method.

Even better, create a singleton object that does the serverSync and your view controller can access and use your server data from there.

You can use NSNotificationCenter for this. Here is the example.

In your 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.
}

In your 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!");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM