【问题标题】:tabBar didSelectItem seems not to be workingtabBar didSelectItem 似乎不起作用
【发布时间】:2012-06-08 09:14:55
【问题描述】:

在我的头文件中我有这个:

@interface TabBarController : UIViewController <UIApplicationDelegate, UITabBarDelegate, UITabBarControllerDelegate>{

    IBOutlet UITabBarController *tabBarController;

}

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

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

在我的主文件中,我有这个:

@synthesize tabBarController;

-(void)viewDidLoad{
    [super viewDidLoad];
    self.tabBarController.delegate = self;
    self.view = tabBarController.view;
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSLog(@"rawr"); 
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)dealloc {
    [tabBarController release];
    [super dealloc];
}


@end

我已经将我的tabbarcontroller 作为代理连接到接口构建器中的文件所有者,但它仍然从未调用didSelectItem 方法。

这里有什么我遗漏的吗?

我已经加了tabBarController.delegate = self;,还是不行。

【问题讨论】:

  • 如果您以编程方式执行此操作会怎样?即tabBarController.delegate = self;?
  • 它仍然不起作用:(

标签: iphone ios xcode uitabbar


【解决方案1】:
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;

这个方法是UITabBar的委托方法,不是UITabBarController,所以

self.tabBarController.delegate = self;

不会工作。

标签栏控制器有自己的UITabBar,但不允许更改标签栏控制器管理的标签栏的委托,所以试试UITabBarControllerDelegate方法吧:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

【讨论】:

  • 不要忘记将&lt;UITabBarControllerDelegate&gt; 添加到您的应用委托的.h 中,并在didFinishLaunchingWithOptions 方法中添加类似UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; tabBarController.delegate = self; 的内容。
【解决方案2】:

你需要添加这个:

tabbarcontroller.delegate = self;

【讨论】:

  • 尝试添加:-(void)viewDidLoad{ [super viewDidLoad]; tabBarController.delegate = self; self.view = tabBarController.view; } 还是不行
【解决方案3】:

使用UITabBarControllerDelegate 代替UITabBarDelegate
-tabBarController:didSelectViewController{} 代替tabBar:didSelectItem{}

界面

@interface TabBarController : UIViewController <UIApplicationDelegate, UITabBarControllerDelegate, UITabBarControllerDelegate>{

    IBOutlet UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end

主文件

@implementation TabBarController
    @synthesize tabBarController;

    /*other stuff*/
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
        NSLog(@"rawr"); 
    }
    /*other stuff*/
@end

【讨论】:

  • 我自己也遇到了这个问题,这个解决方案效果很好。谢谢!
【解决方案4】:

只需为标签栏设置委托。您可以通过设置 self.tabBarController.delegate = self; 或使用界面生成器对 tabbarcontroller 对象(不是 bar)执行此操作,查看连接检查器并将连接拖到文件所有者上。

【讨论】:

  • 可能我想通了。如果你想要方法 -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;要被调用,您应该将 tabbar 委托属性设置为 self.所以尝试类似 self.tabBarController.tabBar.delegate = self;
  • 它不起作用,如果我把 self.tabBarController.tabBar.delegate = self;在 viewDidLoad 中。由于未捕获的异常“NSInternalInconsistencyException”,它给了我一个错误终止应用程序,原因:“不允许更改由标签栏控制器管理的标签栏的委托。”
  • 我在考虑。这里是问题..您想要接收事件的标签栏由 uitabbarviewcontroller 管理。这意味着您将永远不会收到这些消息。您只能收听间接来自标签栏委托方法的 UITabBarViewController 委托方法。 developer.apple.com/library/ios/#documentation/UIKit/Reference/…
  • 我对此还是有点陌生​​,所以有什么建议可以告诉您如何解决这个问题吗?这真的很有帮助。谢谢!
【解决方案5】:

你已经合成了标签栏,所以你现在需要写:self.tabBarController.delegate = self;

【讨论】:

  • 尝试添加:-(void)viewDidLoad{ [super viewDidLoad]; self.tabBarController.delegate = self; self.view = tabBarController.view; } 还是不行
【解决方案6】:

如上所述,它可能无法正常工作的原因有很多。这是一个更微妙的原因。如果您以编程方式创建 UI(没有情节提要或 Xib),则需要设置 UIWindow 的屏幕。

self.window = [[UIWindow alloc] init];
self.window.screen = [UIScreen mainScreen];  // <- The UITabViewController will not
                                             //    accept taps without this.

如果您使用的是 Xib,我相信这相当于选择了“启动时全屏”。必须检查一下,否则我注意到我的选项卡不起作用。

更新:无论如何,如果你必须投反对票,但我有一个不工作的标签控制器,这是我必须做的才能让它工作。此页面上的其他答案都没有帮助我。我想这些天使用 xib/storyboard 非常少见(我认为这是从 iOS 5 天开始的),但留给那些遇到这种情况的人。

【讨论】:

    猜你喜欢
    • 2016-11-29
    • 2016-02-01
    • 2020-09-23
    • 2010-12-05
    • 2011-06-14
    • 2015-01-10
    • 2016-02-24
    • 2011-01-18
    • 2018-06-20
    相关资源
    最近更新 更多