Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 以编程方式添加TabBarController_Iphone_Objective C_Ios_Xcode_Tabbarcontroller - Fatal编程技术网

Iphone 以编程方式添加TabBarController

Iphone 以编程方式添加TabBarController,iphone,objective-c,ios,xcode,tabbarcontroller,Iphone,Objective C,Ios,Xcode,Tabbarcontroller,我想以编程方式创建一个选项卡栏控制器和导航控制器。我的代码工作到目前为止,它在底部显示一个选项卡栏,但OptionViewController在第二个选项卡栏的按钮上没有显示任何内容(没有标题)。有趣的是,当我在没有任何内容的情况下点击按钮时,标题就会出现(他的观点也是如此),有人能向我解释一下我做错了什么吗?我尝试使用以下代码: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSD

我想以编程方式创建一个选项卡栏控制器和导航控制器。我的代码工作到目前为止,它在底部显示一个选项卡栏,但OptionViewController在第二个选项卡栏的按钮上没有显示任何内容(没有标题)。有趣的是,当我在没有任何内容的情况下点击按钮时,标题就会出现(他的观点也是如此),有人能向我解释一下我做错了什么吗?我尝试使用以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2];

    DefaultViewController *dvc = [[DefaultViewController alloc] init];
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc];
    [tabItems addObject:dvc_nc];
    [dvc release];
    [dvc_nc release];

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc];
    [tabItems addObject:ovc_nc];
    [ovc release];
    [ovc_nc release];

    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = tabItems;
    self.tabController = tbc;
    [tabItems release];
    [tbc release];

    [self.window addSubview:self.tabController.view];

    return YES;
}

您需要设置
UINavigationController
的选项卡项和标题,而不是其根视图控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2];

    DefaultViewController *dvc = [[DefaultViewController alloc] init];
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc];
    dvc_nc.tabBarItem.title = @"Default";
    dvc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"]];
    [tabItems addObject:dvc_nc];
    [dvc release];
    [dvc_nc release];

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc];
    ovc_nc.tabBarItem.title = @"Option"
    ovc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Optiomn" ofType:@"png"]];

    [tabItems addObject:ovc_nc];
    [ovc release];
    [ovc_nc release];

    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = tabItems;
    self.tabController = tbc;
    [tabItems release];
    [tbc release];

    [self.window addSubview:self.tabController.view];

    return YES;
}

如果有人需要快速版本。这对我有用。感谢@rckoenes提供我用来翻译的objC答案

    window?.makeKeyAndVisible()

    let dvc = HomeViewController()
    let dvc_nc = UINavigationController(rootViewController: dvc)
        dvc_nc.tabBarItem.title = "Home"
        dvc_nc.tabBarItem.image = UIImage(named: "HomeIcon")
    controllers.append(dvc_nc)

    let ovc = ProfileViewController()
    let ovc_nc = UINavigationController(rootViewController: ovc)
        ovc_nc.tabBarItem.title = "Profile"
        ovc_nc.tabBarItem.image = UIImage(named: "ProfileIcon")
    controllers.append(ovc_nc)

    let tbc = UITabBarController()
        tbc.viewControllers = controllers

    window?.rootViewController = tbc

    UINavigationBar.appearance().tintColor = UIColor(red: 0.05, green: 0.47, blue: 0.91, alpha: 1.0)
    UINavigationBar.appearance().barTintColor = UIColor(red: 0.05, green: 0.47, blue: 0.91, alpha: 1.0)
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent

我使用UIViewController的
UINavigationController
创建了
UITabbarController
作为应用程序的rooview控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2];

    DefaultViewController *dvc = [[DefaultViewController alloc] init];
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc];
    dvc_nc.tabBarItem.title = @"Default";
    dvc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"]];
    [tabItems addObject:dvc_nc];
    [dvc release];
    [dvc_nc release];

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc];
    ovc_nc.tabBarItem.title = @"Option"
    ovc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Optiomn" ofType:@"png"]];

    [tabItems addObject:ovc_nc];
    [ovc release];
    [ovc_nc release];

    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = tabItems;
    self.tabController = tbc;
    [tabItems release];
    [tbc release];

    [self.window addSubview:self.tabController.view];

    return YES;
}
这里还有一个例子: 我使用XIB作为视图控制器

AppDelegate.m

我创建了一个方法名:
setupAppHome

#pragma mark - SETUP HOME
-(void) setupAppHome{
    NSLog(@"set up the nano home");

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    if (_chatViewController == nil) {
        _chatViewController = [[ChatViewController alloc] initWithNibName:@"ChatViewController" bundle:nil];
        chatNav = [[UINavigationController alloc] initWithRootViewController:_chatViewController];
        chatNav.tabBarItem.title=@"Chat";
        chatNav.tabBarItem.image=[UIImage imageNamed:@"chat_icon.png"];

    }
    if (_callController == nil) {
        _callController = [[CallViewController alloc] initWithNibName:@"CallViewController" bundle:nil];
        callNav = [[UINavigationController alloc] initWithRootViewController:_callController];
        callNav.tabBarItem.title=@"Call";
        callNav.tabBarItem.image=[UIImage imageNamed:@"call_icon.png"];

    }
    if (_contanctsController == nil) {
        _contanctsController = [[ContactsViewController alloc] initWithNibName:@"ContactsViewController" bundle:nil];
        conNav = [[UINavigationController alloc] initWithRootViewController:_contanctsController];
        conNav.tabBarItem.title=@"Contact";
        conNav.tabBarItem.image=[UIImage imageNamed:@"contact_icon.png"];

    }
    if (_settingController == nil) {
        _settingController = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
        settingNav = [[UINavigationController alloc] initWithRootViewController:_settingController];
        settingNav.tabBarItem.title=@"Setting";
        settingNav.tabBarItem.image=[UIImage imageNamed:@"setting_icon.png"];

    }

    self.tabController = [[UITabBarController alloc] init];

    NSMutableArray          *controllers = [[NSMutableArray alloc] initWithCapacity:4];
    [controllers addObject:chatNav];
    [controllers addObject:callNav];
    [controllers addObject:conNav];
    [controllers addObject:settingNav];


    self.tabController.viewControllers = controllers;//@[chatNav,callNav,conNav,settingNav];

    self.tabController.selectedIndex=0;



    [self.window setRootViewController:self.tabController];
    [self.window makeKeyAndVisible];


}

它是在带有iOS 11的Xcode 9中发送文本的。

我认为您需要在选项卡栏控制器中添加UINavigationController作为子视图,控制类super类作为UINavigationController。问题只是缺少标题,对吗?您在哪里设置
选项ViewConbTroller
标题?如果未在
init
-方法中设置标题,则TabBarController仅从选项VC中读取空标题。我猜你是在像
viewDidLoad
这样的东西中设置title属性?我猜不是,因为:[tbc.view addSubview:ovc_nc.view];使屏幕完全为空!@托马斯,我确实在视图中设置了标题。当我设置时,有趣的是,当我点击tabbar项时,标题突然出现!我已经更新了我的帖子,它将向您展示如何设置tabBarItem。现在它正确地显示了标题,但现在当我在选项视图中设置标题为“选项”时,我使用您的方法将其初始化为“选项”,因此当应用程序启动时,我会看到“默认”和“选项”。当我点击TabBar项时,它会将标题更改为我在viewDidLoad中配置的self.title!不要在viewDidLoad中设置标题。哈哈,那么我的导航栏就没有标题了!我将其从设置self.title改为self.navigationItem.title=@“选项”!非常感谢。