【发布时间】:2014-06-20 03:00:45
【问题描述】:
我试图在applicationDidEnterBackground 方法中从AppDelegate.m 调用我的ViewController.m 的现有方法,所以我找到了这个链接:Calling UIViewController method from app delegate,它告诉我要实现这个代码:
在我的 ViewController.m 中
-(void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.myViewController = self;
}
在我的 AppDelegate 中:
@class MyViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (weak, nonatomic) MyViewController *myViewController;
@end
在 AppDelegate 的实现中:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self.myViewController method];
}
所以我把这段代码放在我的项目中,它运行良好,但我不明白代码是如何工作的,一行一行。 sharedApplication 有什么作用?为什么我必须设置一个委托而不是仅仅创建一个 ViewController 的实例,比如:
ViewController * instance = [[ViewController alloc] init];
[instance method];
【问题讨论】:
标签: ios iphone objective-c uiviewcontroller appdelegate