我想删除这条线警告: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 * .....' –