TabBarController 委托不起作用

发布于 2024-08-23 09:18:43 字数 293 浏览 5 评论 0原文

任何人都可以帮助我吗 当我使用 UITabBarController 委托时它不起作用..

我调用了这样的委托方法..

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

 [self.navigationController popToRootViewControllerAnimated:NO];
}

Can any one help me,
when i am using my UITabBarController delegate it is not working..

I called a delegate method like this..

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

 [self.navigationController popToRootViewControllerAnimated:NO];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

你的笑 2024-08-30 09:18:44

阅读文档以更深入地了解导航控制器之间的关系, tabBar 控制器,以及视图和导航层次结构。

然后检查您提供的代码。哪个视图/控制器是容器?您正在弹出 self 的 navigationController,它与 tabBarController 不同。如果您想在选项卡之间切换,我认为您实际上并不需要此方法。

尝试注释掉这个方法。它是 UITabBarController 委托协议中的一个可选方法。如果将其注释掉,您应该获得选项卡控制器的默认行为,即选择适当的 viewController 并切换到新视图。

通常,仅当您希望在视图控制器之间切换时执行某些操作时,才需要使用此方法。

Read the documents to get a deeper understanding of the relationships between navigation controllers, tabBar controllers, and the view and navigation hierarchy.

Then review the code you've provided. Which view/controller is the container? You are popping the navigationController of self, which is not the same as the tabBarController. I don't think you actually need this method if you are looking to switch between tabs.

Try commenting out this method. It is an optional method in the UITabBarController delegate protocol. If you comment it out, you should get the default behavior of the tab controller, which should be to select the appropriate viewController and switch to the new view.

You typically only need to use this method if you want some action taken as you switch between view controllers.

潜移默化 2024-08-30 09:18:43

如果您正在做的是子类化 UITabBarController,那么...奇怪的是...您可以通过将自身设置为委托来使其工作:

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

然后 didSelectViewController 操作将正常触发:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"View Changed");
}

不要忘记将 UITabBarControllerDelegate 类添加到你的 .h 文件:

@interface MyTabBarController : UITabBarController <UITabBarControllerDelegate>

@end

If what you're doing is subclassing a UITabBarController, then... oddly enough... you can get it working by setting itself as a delegate:

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

Then the didSelectViewController action will fire normally:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"View Changed");
}

Don't forget to add your UITabBarControllerDelegate class to your .h file:

@interface MyTabBarController : UITabBarController <UITabBarControllerDelegate>

@end
甜警司 2024-08-30 09:18:43

如果您通过扩展 UITabBarController 并尝试以编程方式更改选项卡栏选定索引来使用选项卡栏自定义,那么它将不会调用委托。

请参阅“UITabBarDelegate”内的注释:

// Note: called when a new view is selected by the user (but not programmatically)

If you are using tab bar customizing by extending UITabBarController and trying to change tab bar selected index programmatically then it will not call delegates.

Please see the note inside "UITabBarDelegate":

// Note: called when a new view is selected by the user (but not programmatically)
哎呦我呸! 2024-08-30 09:18:43

这可能对你有帮助

-(void)applicationDidFinishLaunching:(UIApplication *)application {
    tabBarController.delegate=self;
    // Add the tab bar controller's current view as a subview of the window
    [window addSubview:tabBarController.view];
}

This might help you

-(void)applicationDidFinishLaunching:(UIApplication *)application {
    tabBarController.delegate=self;
    // Add the tab bar controller's current view as a subview of the window
    [window addSubview:tabBarController.view];
}
逆夏时光 2024-08-30 09:18:43

指定UITabbarcontrollerDelegate

在 .h 文件中

然后

-(void)applicationDidFinishLaunching:(UIApplication *)application {

tabBarController.delegate=self;

// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}

specify

UITabbarcontrollerDelegate in .h file

then

-(void)applicationDidFinishLaunching:(UIApplication *)application {

tabBarController.delegate=self;

// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文