2013-06-11 10 views
6

के लिए प्रोग्रामेटिक रूप से टैब बार बना रहा है। मैं अपने दृश्य नियंत्रक को प्रोग्राम बार में एक टैब बार जोड़ रहा हूं क्योंकि स्क्रोल व्यू होने के कारण मैं इसे अपने दृश्य के बीच में नहीं रख सकता। मैं इसे जोड़ने के बारे में उलझन में हूं। क्या इसे मेरे ViewDidLoad विधि या मेरे AppDelegate में शुरू करने की आवश्यकता है? तो मेरे पास है:व्यू कंट्रोलर

UITabBarController *tabBar = [[UITabBarController alloc] init]; 
[self.view addSubview:tabBar.view]; 
[tabBar release]; 

कैसे मैं इसे अपने scrollview की तह तक आवंटित कर सकते हैं?

धन्यवाद!

अब

मेरे AppDelegate कक्षा में:

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

    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    UITabBarController *tabBarController = [[UITabBarController alloc] init]; 

    ViewController* vc = [[ViewController alloc] init]; 


    NSArray* controllers = [NSArray arrayWithObjects:vc, tabBarController, nil]; 
    tabBarController.viewControllers = controllers; 

    [_window addSubview:tabBarController.view]; 

    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 

} 

यह दुर्घटनाग्रस्त रहा है और यकीन नहीं है अगर यह एक रिहाई मैं याद कर रहा हूँ है।

संपादित करें: किसी और AppDelegate.m में इस सोच के लिए:

self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 
self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4]; 
+0

'एनएसएआरएआरए * नियंत्रक = [एनएसएआरएआरएआरआईआरआईथऑब्जेक्ट्स: वीसी, टैबबार नियंत्रक, शून्य]; 'आप' टैबबार नियंत्रक 'क्यों जोड़ रहे हैं? – Bhavin

+0

हां मैंने अपने प्रश्न में संपादित किया है यह दिखाने के लिए कि मेरे लिए क्या काम किया है –

+0

एक और बात थी और यह था: आपने 'viewControllers' की अपनी सरणी में 'tabBarController' जोड़ा और यह गलत था। मैंने अपने उत्तर में यही जोड़ा है। – Bhavin

उत्तर

7

इस प्रलेखन एप्पल द्वारा दिए गए पर एक नज़र डालें: ViewControllers

नमूना कोड:

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    UITabBarController *tabBarController = [[UITabBarController alloc] init]; 

    FirstViewController* vc1 = [[FirstViewController alloc] init]; 
    SecondViewController* vc2 = [[SecondViewController alloc] init]; 

    NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil]; 
    tabBarController.viewControllers = controllers; 

    // Add the tab bar controller's current view as a subview of the window 
    [window addSubview:tabBarController.view]; 
} 
+0

सिर्फ एक छोटी सी छोटी चीज़: '' '[self.window addSubview: tabBarController.view];' '' – Ben

3

मैं इसे इस तरह से किया था। मैंने इसे अपने ऐप प्रतिनिधि से कॉपी किया और यह ठीक काम करता है। असल में आप टैब बार में व्यू कंट्रोलर जोड़ते हैं और फिर टैब बार को विंडो के सबव्यू में जोड़ते हैं।

का दृष्टांत उदाहरण

iVacationTabBar = [[UITabBarController alloc] init]; 

हालांकि आप विचारों/दृश्य नियंत्रक बनाने के लिए:

UINavigationController *rvc_tools = [[UINavigationController alloc] initWithRootViewController: vc_tools]; 
    UINavigationController *rvc_settings = [[UINavigationController alloc] initWithRootViewController: vc_settings]; 
    UINavigationController *rvc_about = [[UINavigationController alloc] initWithRootViewController: vc_about]; 
    UINavigationController *rvc_currentpage = [[UINavigationController alloc] initWithRootViewController: vc_currentpage]; 
    UINavigationController *rvc_backup = [[UINavigationController alloc] initWithRootViewController:vc_backup]; 

सरणी नियंत्रक जोड़ें:

NSArray* controllers = [NSArray arrayWithObjects: rvc_currentpage, rvc_tools,rvc_settings, rvc_backup, rvc_about, nil]; 

दृश्य नियंत्रकों की सरणी सेट आपके टैब बार में:

[iVacationTabBar setViewControllers: controllers animated:NO]; 

विंडो के सबव्यू में टैब बार जोड़ें।

[_window addSubview:iVacationTabBar.view]; 
0

आप एक UIViewController पर एक UITabbarController नहीं जोड़ सकते। इसके बजाय, ViewController पर आपको TabbarController दिखाना होगा, TabbarController बनाएं, इसके लिए ViewController एस सेट करें, और इसे Window पर जोड़ें।

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