2013-10-07 115 views
0

我一直在這個問題上已經有一段時間了,無法找到解決我的問題。使用故事板與自定義UITabbarController

我有我試圖與圖像來定製的TabBar視圖控制器,我有然而工作我需要使用代碼來顯示和INIT使用TabBar的視圖控制器的自定義圖形。我也有一個問題,顯示導航欄在我的標籤之一的頂部,我認爲是連接到我如何啓動標籤視圖控制器

故事板顯示應該有一個導航欄在頂部藥物選項卡和該視圖通過segue連接到標籤欄 您可以看到我試圖使用storyboard segues將我的視圖控制器鏈接到標籤欄控制器。我在MedicationViewController.m

/ 
// MedicationViewController.m 
// fibromapp 
// 
// Created by jamie mcallister on 08/09/2013. 
// Copyright (c) 2013 Jamie McAllister. All rights reserved. 
// 

#import "MedicationViewController.h" 
#import "TakenViewController.h" 
#import "MedsListViewController.h" 
#import "MedsAlarmViewController.h" 

@interface MedicationViewController() 

@end 

@implementation MedicationViewController 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 

     TakenViewController *viewController2 = [[TakenViewController alloc] init]; 
     MedsListViewController *viewController1 = [[MedsListViewController alloc] init]; 
     MedsAlarmViewController *viewController3 = [[MedsAlarmViewController alloc] init]; 


     self.viewControllers = [NSArray arrayWithObjects:viewController1, 
           viewController2, 
           viewController3,nil]; 
     UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Medication" image:[UIImage imageNamed:NULL] tag:1]; 
     UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:@"Taken" image:[UIImage imageNamed:NULL] tag:2]; 
     UITabBarItem *tab3 = [[UITabBarItem alloc] initWithTitle:@"Alarms" image:[UIImage imageNamed:NULL] tag:3]; 
     UIImage* sel = [UIImage imageNamed:@"fmtabSel"]; 


     [viewController1 setTabBarItem:tab1]; 
     [viewController2 setTabBarItem:tab2]; 
     [viewController3 setTabBarItem:tab3]; 

     UIImage* tabBarBackground = [UIImage imageNamed:@"fmtab.png"]; 

     UITabBar *tabBar = self.tabBar; 
     [tabBar setBackgroundImage:tabBarBackground]; 
     [tabBar setSelectionIndicatorImage:sel]; 
    } 
    return self; 
} 


- (void)viewDidLoad 
{ 
UITabBar *tabbar = self.tabBar; 
NSLog(@"%f %f", tabbar.frame.size.width, tabbar.frame.size.height);//used to find the size of the bar 
[super viewDidLoad]; 

UIImage* tabBarBackground = [UIImage imageNamed:@"fmtab.png"]; 
UIImage* sel = [UIImage imageNamed:@"fmtabSel"]; 
    UITabBar *tabBar = self.tabBar; 
    [tabBar setBackgroundImage:tabBarBackground]; 
    [tabBar setSelectionIndicatorImage:sel]; 

    // Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

@end 

與此代碼我得到的標籤,但有在該選項卡的頂部沒有導航欄下面的代碼。

任何人可以建議什麼我必須這樣做,以解決這個問題?

如果您需要任何更多信息

隨便問,我將它編輯成這個問題的底部。 在此先感謝:)

回答

0

如果您希望導航欄位於頂部您應該使用導航控制器填充根控制器(而不僅僅是普通控制器)來填充導航控制器。 水木清華這樣的:

TakenViewController *viewController2 = [[TakenViewController alloc] init]; 
MedsListViewController *viewController1 = [[MedsListViewController alloc] init]; 
MedsAlarmViewController *viewController3 = [[MedsAlarmViewController alloc] init]; 
UINavigationController * nc1 = [[UINavigationController alloc] initWithRootViewController:viewController1]; 
UINavigationController * nc2 = [[UINavigationController alloc] initWithRootViewController:viewController2]; 
UINavigationController * nc3 = [[UINavigationController alloc] initWithRootViewController:viewController3]; 

self.viewControllers = [NSArray arrayWithObjects:nc1, 
           nc2, 
           nc3,nil]; 
+0

謝謝你,這代碼似乎已經奏效,但它看起來像我的應用程序,現在依靠代碼,而不是在一些地區的應用(如導航欄標題)。對我來說有點問題,因爲我在故事板的導航欄中有一個按鈕,可以將項目添加到列表中 –

+0

正如萊昂納多說過的,您可以在故事板中執行此操作。我剛剛向你展示了一個移動方向 - 帶有導航的tabbar。 – Stas

1

爲了有一個導航欄,你必須把使用TabBar控制器和第一之間的UIViewController一個UINavigationController。

所有在故事板來完成,而無需編寫一行代碼的需求。

+0

那就是如何我已經奠定了我的故事板,但我還沒有一個導航欄,當我運行應用程序 –

+0

我的錯,我確實現在有一個嘮叨吧,它只是沒有顯示從故事板過這麼難查看 :/ –