0
AppDelegate.h:爲AppDelegate中無可見接口聲明的選擇
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
+ (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title;
AppDelegate.m:
+ (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title {
UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
[MBProgressHUD hideAllHUDsForView:window animated:YES];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
hud.labelText = title;
return hud;
}
在someOtherMethod在AppDelegate.m:
[self showGlobalProgressHUDWithTitle:@"Checking for Updates"]; //No visible interface for AppDelegate declares the selector 'showGlobalProgressHUDWithTitle:'
爲什麼?界面中的其他方法是可見的,但爲什麼不是這個?它與做類方法有關嗎?
+是一個類方法, - 是一個實例方法。你不用自己執行這個方法,你可以在AppDelegate上執行它。您可能想要將定義更改爲 - 。 –
@StefanH有道理,但爲什麼我不能這樣做(從不同的類):AppDelegate * appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]; [appDelegate showGlobalProgressHUDWithTitle:@「whatever」]; – soleil
因爲[[[UIApplication sharedApplication]委託]'返回'AppDelegate'的一個實例,而不是類本身。要調用這個類方法,你需要執行'[AppDelegate classMethodName]'。 – Rog