简体   繁体   English

如何通过委托在类上调用方法(多任务方法)?

[英]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. 正如Vin所提到的,您可以将其添加到applicationWillEnterForeground方法中。 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. 要更新UI,可以将应用程序委托设置为对视图控制器具有引用,然后从那里调用update方法。 Or better yet, you can use NSNotificationCenter to simply notify your other classes of the update. 或者更好的是,您可以使用NSNotificationCenter只是将更新通知给其他类。

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 SampleAppDelegate.h

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

SampleAppDelegate.m 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. 然后,无论在何处加载视图控制器,都可以分配应用程序委托的sampleViewController属性的值。 So, for example, if you initialized your view controller programmatically on the app delegate's didFinishLaunchingWithOptions method, just assign it there. 因此,例如,如果您在应用程序委托的didFinishLaunchingWithOptions方法上以编程方式初始化了视图控制器,则只需在其中分配它即可。

- (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. 如果在应用程序委托之外加载了视图控制器,则需要通过sharedApplication的委托属性访问应用程序委托。

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

You can then call the update method from the applicationWillEnterForeground method. 然后,您可以从applicationWillEnterForeground方法调用update方法。

- (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. applicationWillEnterForeground在应用程序委托时,你的应用程序仅仅是即将来到的前景将被调用。 Write your refresh logic there. 在此处编写刷新逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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