如何从我的视图控制器 class 访问在 AppDelegate class 中声明的对象?
How can I access an object declared in the AppDelegate class from my view controller class?
这是我的 AppDelegate.h
:
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
ProductHandlerModel *handler;
}
@property (strong, nonatomic) UIWindow *window;
-(ProductHandlerModel*)getHandler;
@end
这是我的 AppDelegate.m
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
handler = [[ProductHandlerModel alloc]init];
return YES;
}
- (ProductHandlerModel*)getHandler {
return handler;
}
如何从另一个 UIViewController
class 访问此处理程序对象?
像
一样打电话
AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate getHandler];
或者像
一样直接打电话
[(AppDelegate *)[[UIApplication sharedApplication] delegate] getHandler];
别忘了导入。
#import "AppDelegate.h"
这是我的 AppDelegate.h
:
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
ProductHandlerModel *handler;
}
@property (strong, nonatomic) UIWindow *window;
-(ProductHandlerModel*)getHandler;
@end
这是我的 AppDelegate.m
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
handler = [[ProductHandlerModel alloc]init];
return YES;
}
- (ProductHandlerModel*)getHandler {
return handler;
}
如何从另一个 UIViewController
class 访问此处理程序对象?
像
一样打电话AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate getHandler];
或者像
一样直接打电话[(AppDelegate *)[[UIApplication sharedApplication] delegate] getHandler];
别忘了导入。
#import "AppDelegate.h"