gpt4 book ai didi

ios - UITabBarController 委托(delegate)方法永远不会被调用

转载 作者:可可西里 更新时间:2023-11-01 04:30:08 26 4
gpt4 key购买 nike

我目前正在重构我的 iPad 应用程序,而不是 NavigationBar 上的按钮,我想将我的注销按钮作为 TabBar 按钮项。

我所有的 View 都在一个独特的 StoryBoard 中,所以我通过这种方式在我的 Appdelegate.m 中获取了我的 TabBar :

// Add logout tab to tabbar
storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabbar = (UITabBarController *)[storyboard instantiateViewControllerWithIdentifier:@"tabbar"];
[tabbar setDelegate:self];

当然委托(delegate)协议(protocol)是在我的Appdelegate.h中声明的

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

之后,我创建一个空的 viewController 以创建我的注销选项卡:

UIViewController *logout        = [[UIViewController alloc]init];

然后我将 tabbar viewControllers 作为 NSMutableArray 并添加我的注销 VC:

NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[tabbar viewControllers]];

[viewControllers addObject:logout];

最后我用我的新数组设置了 tabbar viewControllers :

[tabbar setViewControllers:viewControllers];

我认为这些步骤是正确的。那么为什么当我更改显示的选项卡时没有调用 didSelectViewController 呢?

如果有帮助,这是我的 didSelectViewController 方法(写在 AppDelegate.m 中)

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(@"onglet sélectionné : %d / %d", [tabBarController selectedIndex], [[tabBarController viewControllers]count]);
//select the index where your logout button is
if ([tabBarController selectedIndex] == [[tabBarController viewControllers]count]-1) {
NSLog(@"logout");
}
}

在此先感谢您的帮助!

最佳答案

[self setDelegate:self]; 放在你的 ViewDidLoad 或对象初始化的地方

关于ios - UITabBarController 委托(delegate)方法永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21549642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com