0

因此,我一直在尋找各地的示例如何創建一個uitabbar prpgrammatically。我盡我所能從每個示例中獲取所需的內容,並將我希望我的應用程序的外觀如下所示:在2選項卡欄後面的歡迎屏幕。無法讓我的uitabbar以編程方式設置時出現

我有一個UIButton的歡迎屏幕上移動視圖控制器:

-(IBAction)aMethod:(id)sender { 

    MyTabProjectViewController *controller = [[MyTabProjectViewController alloc] initWithNibName:nil bundle:nil]; 
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentModalViewController:controller animated:YES]; 
    } 

Then in my MyTabProjectViewController.m I do this: 


    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    //Setting up the view 
    UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    contentView.backgroundColor = [UIColor whiteColor]; 
    self.view = contentView;  

    //Declaring all view controllers 
    FirstView *first = [[FirstView alloc] init]; 
    SecondView *second = [[SecondView alloc] init]; 

    //Set titles for the view controllers 
    first.title = @"First"; 
    second.title = @"Second"; 


    UITabBarController *tabBarController = [[UITabBarController alloc] init]; 
    tabBarController.view.frame = CGRectMake(0, 0, 320, 460); 


    UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:first]; 
    UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:second]; 

    nvc1.navigationBar.barStyle = UIBarStyleBlack; 
    nvc2.navigationBar.barStyle = UIBarStyleBlack; 


    NSArray *controllers = [[NSArray alloc] initWithObjects:nvc1, nvc2, nil]; 

    self.tabBarController.viewControllers = controllers; 

    [self.view addSubview:tabBarController.view];  
    } 

從某種原因,沒有什麼作品。當我點擊按鈕去MyTabProjectViewController時,我看到一個空白頁面。

+0

我看到你的方法泄漏,試圖釋放控制器[控制器釋放];後presentModal ... – Mat

+0

使用垃圾收集...沒有泄漏:)謝謝! – TommyG

+0

你必須釋放它,我認爲你必須在contentView,第一,第二,tabBarController,nvc1,nvc2和控制器上調用release;如果你正在實例化一個非ivar,爲什麼你在使用它之後不會釋放它? – Mat

回答

0

嘗試添加

tabBarController.selectedIndex = 0; 
0

您實例化的UITabBarController的新實例並設置ViewControllers列表到另一個實例,只需更換行:

self.tabBarController.viewControllers = controllers; 

與線:

tabBarController.viewControllers = controllers; 
相關問題