我想刪除這條線警告:IOS,刪除警告?
UIViewController * appController = [[[UIApplication sharedApplication] delegate] viewController];
我得到這一行代碼的警告是: 「實例方法‘的viewController’未找到(返回類型默認爲‘身份證’ )
有人能幫助解釋如何我可能會刪除這個虧
非常感謝,
我想刪除這條線警告:IOS,刪除警告?
UIViewController * appController = [[[UIApplication sharedApplication] delegate] viewController];
我得到這一行代碼的警告是: 「實例方法‘的viewController’未找到(返回類型默認爲‘身份證’ )
有人能幫助解釋如何我可能會刪除這個虧
非常感謝,
您需要投下您的應用程序委託:
UIViewController * appController = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] viewController];
添加投放到您的委託:?
UIViewController * appController = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] viewController];
我不知道你的...AppDelegate
是如何被調用的,所以我在這裏使用DemoAppDelegate
。
UIViewController *appController = [(DemoAppDelegate *)[[UIApplication sharedApplication] delegate] viewController];
類型轉換是嘈雜,可能是危險的。爲了解決這個問題,我通常最終創建一個簡單的類方法。假設類是一直打算用作應用程序的委託,你可以寫這樣的事情:
// MONApplicationDelegate.h
@interface MONApplicationDelegate : NSObject <UIApplicationDelegate>
// ...
+ (MONApplicationDelegate *)sharedApplicationDelegate;
- (UIViewController *)viewController;
@end
// MONApplicationDelegate.m
@implementation MONApplicationDelegate
// ...
+ (MONApplicationDelegate *)sharedApplicationDelegate
{
id ret = [[UIApplication sharedApplication] delegate];
if (nil == ret) {
assert(0 && "oops - app delegate has not been created yet");
return nil;
}
if (![ret isKindOfClass:[MONApplicationDelegate class]]) {
assert(0 && "invalid class type for shared application delegate");
return nil;
}
else {
return ret;
}
}
@end
// in action
UIViewController * appController =
[[MONApplicationDelegate sharedApplicationDelegate] viewController];
// which is shorter and safer to use, when compared to:
UIViewController * appController =
[(MONApplicationDelegate*)[[UIApplication sharedApplication] delegate] viewController];
你的括號內是在錯誤的地方 - 看其他答案 – jrturton
這怎麼可能downvoted時,它的另一個相同,upvoted的答案? –
它只是原始代碼的一部分。你還會注意到它不包含'UIViewController * .....' –