gpt4 book ai didi

ios - 以编程方式创建 uiTabBarController

转载 作者:技术小花猫 更新时间:2023-10-29 11:07:43 28 4
gpt4 key购买 nike

我想为 UITabBarController 创建一个 UIView

这是我为 .h 文件编写的代码:

@interface TE : UIViewController <UITabBarControllerDelegate>{
UITabBarController *tabBarController;
}
@property (nonatomic,retain) UITabBarController *tabBarController;
@end

viewDidLoad 方法:

UIViewController *testVC = [[T1 alloc] init];
UIViewController *otherVC = [[T2 alloc] init];
NSMutableArray *topLevelControllers = [[NSMutableArray alloc] init];
[topLevelControllers addObject: testVC];
[topLevelControllers addObject: otherVC];
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
[tabBarController setViewControllers:topLevelControllers animated:NO];
tabBarController.selectedIndex = 0;
self.view = tabBarController.view;

这创建了标签栏 Controller ,但是当我点击标签栏项目时,我得到一个错误:

Thread1:Program receive signal: SIGABRT

编辑:我通过下载并修改http://www.iphonedevcentral.com/create-uitabbarcontroller/的版本解决了问题

最佳答案

您在上面说您不想在 appDelegate 中创建 tabBarController。为什么不?您还会在哪里创建它? tabBarController 必须是 Root View Controller ,不能是任何其他 View Controller 的子级。

顺便说一句,确保你实现了:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

NSUInteger tabIndex = [tabBarController.viewControllers indexOfObject:viewController];

if (viewController == [tabBarController.viewControllers objectAtIndex:tabIndex] ) {
return YES;
}

return NO;

}

关于ios - 以编程方式创建 uiTabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8164356/

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