在單個課堂上?讓它成爲這個類的一個屬性。
//.h
@property Whatever *boom;
//.m
- (id)init {
self = [super init];
if (self) {
_boom = [[Whatever alloc] init];
}
return self;
}
整個應用程序?在某個地方創建一個實例,如您的應用程序委託,然後將其傳遞給根視圖控制器,然後將其傳遞給每個視圖控制器。
// AppDelegate .m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// app setup code
Whatever *boom = [[Whatever alloc] init];
FirstViewController vc = self.window.rootViewController;
vc.boom = boom;
}
// FirstViewController.h, NextViewController.h, etc.
@property Whatever *boom;
// FirstViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NextViewController *nextVC = sender.destinationViewController;
nextVC.boom = self.boom;
}
你也可以去辛格爾頓路線,但你是緊耦合的類應用範圍的單個實例。