Nazar Vozniy
Nazar Vozniy

Reputation: 51

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?

Upvotes: 0

Views: 146

Answers (2)

Nilesh Jha
Nilesh Jha

Reputation: 1636

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

Upvotes: 0

Michael Dautermann
Michael Dautermann

Reputation: 89569

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.

Upvotes: 2

Related Questions