2011-09-06 8 views
6

プログラムでタブバーコントローラとナビゲーションコントローラを作成したいと思います。私のコードはこれまでのところ、下部にタブバーが表示されていますが、OptionViewControllerは2番目のタブバーのボタンに何も(タイトルなし)は表示されません。面白いのは、ボタンを何もクリックせずにクリックすると、タイトルが表示されます(彼の見解も同じです)。誰かが私に間違っていることを説明することができますか?あなたがtabBarItemとUINavigationControllerはなく、ルートのViewControllerのタイトルを設定する必要がTabBarControllerをプログラムで追加する

- (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; 
} 
+0

とXcodeの9に尋ねてみている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]; [email protected]"Chat"; chatNav.tabBarItem.image=[UIImage imageNamed:@"chat_icon.png"]; } if (_callController == nil) { _callController = [[CallViewController alloc] initWithNibName:@"CallViewController" bundle:nil]; callNav = [[UINavigationController alloc] initWithRootViewController:_callController]; [email protected]"Call"; callNav.tabBarItem.image=[UIImage imageNamed:@"call_icon.png"]; } if (_contanctsController == nil) { _contanctsController = [[ContactsViewController alloc] initWithNibName:@"ContactsViewController" bundle:nil]; conNav = [[UINavigationController alloc] initWithRootViewController:_contanctsController]; [email protected]"Contact"; conNav.tabBarItem.image=[UIImage imageNamed:@"contact_icon.png"]; } if (_settingController == nil) { _settingController = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil]; settingNav = [[UINavigationController alloc] initWithRootViewController:_settingController]; [email protected]"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]; } 

を私はあなたにサブビューとしてUINavigationControllerを追加する必要があると思いますUINavigationController –

+0

のような制御クラスのスーパークラスを持つTab Barコントローラは、欠落しているタイトルのみです。あなたの 'OptionsViewConbtroller'の' title'はどこで設定していますか?あなたの 'init'メソッドにタイトルを設定していない場合、TabBarControllerはあなたのOptionsVCから空のタイトルだけを読み込みます。あなたはsthのtitleプロパティを設定していると思います。 'viewDidLoad'のように? – thomas

+0

私はそう思っていません、これは:[tbc.view addSubview:ovc_nc.view];画面が完全に空になります! – Mark

答えて

10

:私は、次のコードを使用しようとしました。

- (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; 
} 
+0

あなたは何を意味するのかを正確に示すコードをお持ちですか? – Mark

+0

投稿を更新しました。tabBarItemの設定方法が表示されます。 – rckoenes

+0

これでタイトルが正しく表示されますが、 "Options"のオプションビューでタイトルを設定すると、 "Option"でメソッドを初期化するので、アプリケーションが起動するときに "Default"と "Option"が表示されます。 TabBar項目をクリックすると、self.DitLoadでselfTitleを設定したタイトルに変更されます。 – Mark

0

誰かがSWIFTバージョンを必要とする場合。これは私のために働いた。私はこれを翻訳するために私が使用したobjC答えのための@rckoenesに感謝します。

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 
3

私のUIViewControllerためUINavigationControllerとアプリのrooviewコントローラとしてUITabbarControllerを作成しました。

もう1つの例: 私はView Controllerにxibsを使用しました。

AppDelegate.m

私は、メソッド名を作成:これはiOSの11

関連する問題