2011-09-06 34 views
6

Tôi muốn tạo bộ điều khiển thanh tab và bộ điều khiển điều hướng theo lập trình. Mã của tôi làm việc cho đến nay nó cho thấy một thanh tab ở phía dưới, nhưng OptionViewController không nói bất cứ điều gì (không có tiêu đề) trên nút của thanh tab thứ hai. Điều buồn cười là, khi tôi nhấp vào nút mà không có bất cứ điều gì trên đó, tiêu đề xuất hiện (và như vậy là quan điểm của mình), ai đó có thể giải thích cho tôi những gì tôi đang làm sai? Tôi cố gắng để sử dụng đoạn mã sau:Thêm một TabBarController lập trình

- (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

Tôi nghĩ rằng bạn cần phải thêm UINavigationController như một cái nhìn tiểu trong điều khiển thanh Tab với các lớp kiểm soát siêu lớp như UINavigationController –

+0

vấn đề chỉ là tiêu đề bị thiếu, phải không? bạn đang đặt 'tiêu đề' của' OptionsViewConbtroller' ở đâu? Nếu bạn đang thiết lập tiêu đề không có trong phương thức 'init' của bạn thì TabBarController chỉ đọc một tiêu đề trống từ OptionsVC của bạn. Tôi đoán bạn đang thiết lập các tài sản tiêu đề trong sth. như 'viewDidLoad'? – thomas

+0

Tôi đoán là không, vì điều này: [tbc.view addSubview: ovc_nc.view]; làm cho màn hình hoàn toàn trống rỗng! – Mark

Trả lời

10

Bạn cần phải đặt tabBarItem và tiêu đề của UINavigationController và không viewController gốc của nó.

- (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

Bạn có một số mã để hiển thị chính xác ý bạn không? – Mark

+0

Tôi đã cập nhật bài đăng của mình, nó sẽ chỉ cho bạn cách đặt tabBarItem. – rckoenes

+0

Bây giờ nó hiển thị tiêu đề một cách chính xác, nhưng bây giờ khi tôi đặt tiêu đề trong tùy chọn xem trên "Tùy chọn" và với phương pháp của bạn, tôi khởi động "Tùy chọn", vì vậy khi ứng dụng khởi động tôi thấy "Mặc định" và "Tùy chọn". Khi tôi bấm vào mục TabBar nó thay đổi tiêu đề cho những gì tôi cấu hình trong viewDidLoad với self.title! – Mark

0

Nếu ai đó cần phiên bản SWIFT. Điều này làm việc cho tôi. Cảm ơn @rckoenes cho câu trả lời objC tôi đã sử dụng để dịch từ này.

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

tôi tạo ra các UITabbarController như rooview điều khiển của ứng dụng với UINavigationController cho UIViewController.

đây một ví dụ khác: Tôi đã sử dụng xib cho Bộ điều khiển chế độ xem.

AppDelegate.m

tôi có thể tạo một tên phương pháp: 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]; 


} 

Người ta đã nhắn tin cho trong Xcode 9 với iOS 11.

Các vấn đề liên quan