如何符合UITabBarControllerDelegate
我有一个基于选项卡栏的应用程序,并执行以下操作来获取对应用程序委托的引用:
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
然后给出此警告:
warning: type 'id <UIApplicationDelegate>' does not conform to the 'UITabBarControllerDelegate'
我的应用程序委托标头如下所示:
#import <UIKit/UIKit.h>
@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
.m 文件中唯一的方法是 applicationDidFinishLaunching 和 dealloc。我还需要什么来遵守协议?
I have a tabbar based application and do the following to get a reference to the application delegate:
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
Which then gives this warning:
warning: type 'id <UIApplicationDelegate>' does not conform to the 'UITabBarControllerDelegate'
My application delegate header looks like this:
#import <UIKit/UIKit.h>
@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
The only methods in the .m file are applicationDidFinishLaunching and dealloc. What else do I need to conform to the protocol?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是静态警告。这意味着
[[UIApplication sharedApplication] delegate]
的返回类型不符合标签栏委托协议,这是正确的。转换从
[[UIApplication sharedApplication] delegate]
返回的值来消除警告:It's a static warning. It means that the return type of
[[UIApplication sharedApplication] delegate]
does not conform to the tab bar delegate protocol, which is true.Cast the value returned from
[[UIApplication sharedApplication] delegate]
to get rid of the warning:如果您使用
声明 MyAppDelegate 符合 UITabBarDelegate,那么您至少需要实现该协议所需的方法。您应该阅读协议的工作原理以及正式协议和非正式协议之间的差异。
您需要实现以符合 UITabBarDelegate 的方法是
If you declare MyAppDelegate as conforming to UITabBarDelegate, using
<UITabBarDelegate>
, then you need to implement at least the required methods of the protocol.You should read up on how protocols work, and the differences between formal and informal protocols.
The method you need to implement to conform to the UITabBarDelegate is
我想插话,因为第二条和第三条评论在技术上是不正确的。该错误表明 AppDelegate 不符合 UITabBarController 委托。有关的答案提到了 UITabBarDelegate。
苹果和橙子。
顺便说一句,Apple 示例代码也使用了强制转换方法。
I would like to chime in because the second and third comments are technically incorrect. The error says that the AppDelegate does not conform to the UITabBarController delegate. The answers about mention the UITabBarDelegate.
Apples and Oranges.
Incidentally, the Apple sample code uses the casting method as well.
“顺便说一下,Apple 示例代码也使用了转换方法。” - 杰夫
我认为这并非偶然。
错误来了,因为委托属性是id“UIApplicationDelegate”,但是有人收到警告,因为他将其分配给MyAppDelegate,这不仅符合UIApplicationDelegate,而且符合UITabBarControllerDelegate。这就是为什么,如果引入演员
MyAppDelegate appDelegate = (MyAppDelegate)[[UIApplication sharedApplication] delegate];
它工作正常。
"Incidentally, the Apple sample code uses the casting method as well." - Jeff
I think that it is not incidentally.
Error comes, because delegate property is id "UIApplicationDelegate", however one obtains warning because he assign it to MyAppDelegate, which is not only conforms to UIApplicationDelegate but also UITabBarControllerDelegate. That is why, if one introduces cast
MyAppDelegate appDelegate = (MyAppDelegate)[[UIApplication sharedApplication] delegate];
it works properly.