如何符合UITabBarControllerDelegate

发布于 2024-08-21 01:06:50 字数 746 浏览 3 评论 0原文

我有一个基于选项卡栏的应用程序,并执行以下操作来获取对应用程序委托的引用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

阳光下的泡沫是彩色的 2024-08-28 01:06:51

这是静态警告。这意味着[[UIApplication sharedApplication] delegate]的返回类型不符合标签栏委托协议,这是正确的。

转换从 [[UIApplication sharedApplication] delegate] 返回的值来消除警告:

MyAppDelegate *appDelegate = (MyAppDelegate*)[[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 *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
很糊涂小朋友 2024-08-28 01:06:51

如果您使用 声明 MyAppDelegate 符合 UITabBarDelegate,那么您至少需要实现该协议所需的方法。

您应该阅读协议的工作原理以及正式协议和非正式协议之间的差异。

您需要实现以符合 UITabBarDelegate 的方法是

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

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

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
孤者何惧 2024-08-28 01:06:51

我想插话,因为第二条和第三条评论在技术上是不正确的。该错误表明 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.

生生漫 2024-08-28 01:06:51

“顺便说一下,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文