2011-09-06 16 views
6

जोड़ना मैं एक टैब बार नियंत्रक और नेविगेशन नियंत्रक प्रोग्रामेटिक रूप से बनाना चाहता हूं। मेरा कोड अब तक काम करता है कि यह नीचे एक टैब बार दिखाता है, लेकिन OptionViewController दूसरे टैब बार के बटन पर कुछ भी नहीं (शीर्षक नहीं) नहीं कहता है। मजाकिया बात यह है कि, जब मैं इसके बिना बटन पर क्लिक करता हूं, तो शीर्षक दिखाई देता है (और उसका विचार भी है), क्या कोई मुझे समझा सकता है कि मैं क्या गलत कर रहा हूं? मैं निम्नलिखित कोड का उपयोग करने की कोशिश की:एक 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 में संदेश भेजा है मैं तुम्हें UINavigationController में एक उप दृश्य के रूप में जोड़ने की जरूरत है लगता है UINavigationController –

+0

के रूप में नियंत्रण कक्षा सुपर क्लास के साथ टैब बार नियंत्रक समस्या केवल गायब शीर्षक है, है ना? आप अपने 'OptionsViewConbtroller' के' शीर्षक 'को कहां सेट कर रहे हैं? यदि आप शीर्षक को अपनी 'init'-method में नहीं सेट कर रहे हैं तो TabBarController केवल आपके OptionsVC से खाली शीर्षक पढ़ता है। मुझे लगता है कि आप sth में शीर्षक-संपत्ति सेट कर रहे हैं। 'viewDidLoad' की तरह? – thomas

+0

मुझे नहीं लगता, क्योंकि यह: [tbc.view addSubview: ovc_nc.view]; स्क्रीन को पूरी तरह से खाली बनाता है! – Mark

उत्तर

10

आप tabBarItem और UINavigationController और नहीं उसकी जड़ ViewController के शीर्षक सेट करना होगा।

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

मैंने अपनी पोस्ट अपडेट की है, यह आपको टैबबर्टम को सेट करने का तरीका दिखाएगी। – rckoenes

+0

अब यह शीर्षक सही तरीके से दिखाता है, लेकिन अब जब मैं "विकल्प" पर विकल्पदृश्य में शीर्षक सेट करता हूं और आपकी विधि के साथ मैं इसे "विकल्प" पर डालता हूं, इसलिए जब एप्लिकेशन बूट हो जाता है तो मुझे "डिफ़ॉल्ट" और "विकल्प" दिखाई देता है। जब मैं टैबबार आइटम पर क्लिक करता हूं तो यह शीर्षक में बदल जाता है जिसे मैंने didLoad में self.title के साथ कॉन्फ़िगर किया है! – Mark

0

अगर किसी को एक स्विफ्ट संस्करण की आवश्यकता है। यह मेरे लिए काम किया। ओबीजेसी उत्तर के लिए धन्यवाद @ रेकोनिस मैं इसका अनुवाद करता था।

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 साथ अनुप्रयोग के नियंत्रक UITabbarController rooview के रूप में बनाया।

यहां एक और उदाहरण: मैंने व्यू कंट्रोलर के लिए xibs का उपयोग किया।

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]; 
     [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]; 


} 

यह आईओएस 11. साथ

संबंधित मुद्दे