简体   繁体   中英

How to call a method on a class through the delegate (multitasking methods)?

I need to call a "refresh method" on one of my classes every time the user returns to my app (multitasking). It seem that for multitasking I need to do this through the app delegate. So what is the best way to call the "refresh method" on my classes self, but from the app delegate?

As mentioned by Vin, you can add it in the applicationWillEnterForeground method. To update your UI, you can set the application delegate to have a reference to your view controllers and call the update method from there. Or better yet, you can use NSNotificationCenter to simply notify your other classes of the update.

If you decided to add a reference to your view controller from the application delegate, you just have to create a property. This is one way to do it. Please note however that it still depends on the structure of your project.

SampleAppDelegate.h

SampleViewController *viewController;
...
@property (nonatomic, retain) SampleViewController *sampleViewController;
...

SampleAppDelegate.m

...
@synthesize sampleViewController;
... 
// don't forget to release in dealloc
[sampleViewController release]

...

You can then assign the value of the app delegate's sampleViewController property wherever you loaded your view controller. So, for example, if you initialized your view controller programmatically on the app delegate's didFinishLaunchingWithOptions method, just assign it there.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    SampleViewController *_sampleViewController = [[SampleViewController alloc] initWithFrame:CGRectMake(0,0,320,480)];
    self.sampleViewController = _sampleViewController;

    [window addSubview:_sampleViewController.view];
    [sampleViewController release];

    [self.window makeKeyAndVisible];
    return YES;
} 

If you loaded the view controller outside of the application delegate, you will need to access the app delegate via the sharedApplication's delegate property.

((SampleAppDelegate*)[UIApplication sharedApplication] delegate).sampleViewController = _sampleViewController;

You can then call the update method from the applicationWillEnterForeground method.

- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of the transition from the background to the active state: here you can undo many of the changes made on entering the background.
     */ 
    [self.sampleViewController updateMyView];   
}

applicationWillEnterForeground in the application delegate will be called when your application is just about to come to foreground. Write your refresh logic there.

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