簡體   English   中英

第二個視圖中的Tabbar

[英]Tabbar in Second View

我的應用程序中有一個激活頁面,每個用戶都必須激活該應用程序。 應用程序激活后,用戶將移至選項卡欄視圖。

我已經創建了一個標簽欄應用程序,從我的激活視圖點擊按鈕我試圖調用標簽欄,我得到一個完整的黑屏。

- (IBAction)moveToActivateView:(id)sender {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UITabBarController *tabBarController = [[UITabBarController alloc]init];
    [self.view addSubview:tabBarController.view];
    appDelegate.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    self.tabBarController.viewControllers = @[viewController1, viewController2];
    appDelegate.window.rootViewController = self.tabBarController;
    [appDelegate.window makeKeyAndVisible];}

嘿在appDelegate中創建tabBarController的屬性並在那里分配所有ViewController然后從yourViewController調用tabBarController

在AppDelegate.h中

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (retain, nonatomic) IBOutlet UITabBarController *tabBarController;

在AppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [self makeTabBar];

    [self addInitialVIew];

    [self.window makeKeyAndVisible];

    return YES;
}  


-(void)makeTabBar
{    
    tabBarController = [[UITabBarController alloc] init];
    tabBarController.delegate=self;
    FirstViewController *firstVC =[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];    

    UINavigationController *firstNC = [[UINavigationController alloc] initWithRootViewController:firstVC];
    firstNC.tabBarItem.title=@"Profile";
    firstVC.tabBarController.tabBar.tag = 0;


    SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    UINavigationController *SecondNavController = [[UINavigationController alloc] initWithRootViewController:secondVC];    
    SecondNavController.tabBarItem.title = @"Search";
    secondVC.tabBarController.tabBar.tag = 1;


    NSArray *viewControllers =[[NSArray alloc]initWithObjects:firstNC,SecondNavController, nil];
    [tabBarController setViewControllers:viewControllers animated:NO];   
}

-(void) addInitialVIew
{    
    InitialViewController *initialViewController = [[InitialViewController alloc]initWithNibName:@"InitialViewController" bundle:nil];
    navigationController = [[UINavigationController alloc]initWithRootViewController:ViewController];
    [self.window addSubview:navigationController.view];
}

現在在InitialViewController您可以添加yourTabBar並刪除InitialViewController

- (IBAction)switchToTabBarBtnPress:(id)sender
{
    AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];

    [[[appdelegte navigationController] view]removeFromSuperview];

    [[appdelegte window]addSubview:[[appdelegte tabBarController]view]];

    [[appdelegte tabBarController]setSelectedIndex:0];
}

並按照我的回答

回答

你有沒有意識到你的本地varialbe tabBarController是不相同,但排序的隱藏屬性self.tabBarController 您創建一個新的UITabBarCotnroller並將其分配給本地變量tabBarController ,該變量只能從此方法訪問。 然后你操縱(添加新創建的視圖控制器)等self.tabBarController Self.tabBarController可以很容易地在這里或其他任何東西。 但它不是你剛剛創建的UITabBarController

它是self.tabBarController (所以可能是nil)你以rootViewController分配給窗口的rootViewController

您確實將tabBarController's視圖作為子視圖添加到self.view。 除此之外,我不知道自己的實際情況是什么,我認為手動添加tabBar的視圖作為子視圖是不是真的很有必要。 設置根視圖控制器(正確)應該足夠了

您想要創建動態tabbar應用程序來解決您的問題。 所以你只需創建基於視圖的應用程序。 在視圖控制器viewdidload方法中放置這些代碼

tabbar1 = [[UITabBarController alloc] init];

    artist_tab_obj = [[artist_tab alloc] initWithNibName:@"artist_tab" bundle:nil];

    UINavigationController *tabItem1 = [[[UINavigationController alloc] initWithRootViewController: artist_tab_obj] autorelease];
    tabItem1.title=@"Artist";
    tabItem1.tabBarItem.image=[UIImage imageNamed:@"Icon1.png"];
    music_tab_obj = [[music_tab alloc] initWithNibName:@"music_tab" bundle:nil];

    UINavigationController *tabItem2 = [[[UINavigationController alloc] initWithRootViewController: music_tab_obj] autorelease];

    tabItem2.title=@"Music";
    tabItem2.tabBarItem.image=[UIImage imageNamed:@"Icon2.png"];

    shout_tab_obj = [[shout_tab alloc] initWithNibName:@"shout_tab" bundle:nil];

    UINavigationController *tabItem3 = [[[UINavigationController alloc] initWithRootViewController: shout_tab_obj] autorelease];

    tabItem3.title=@"Shout";
    tabItem3.tabBarItem.image=[UIImage imageNamed:@"Icon3.png"];
    schedule_tab_obj = [[schedule_tab alloc] initWithNibName:@"schedule_tab" bundle:nil];

    UINavigationController *tabItem4 = [[[UINavigationController alloc] initWithRootViewController: schedule_tab_obj] autorelease];

    tabItem4.title=@"Schedule";
    tabItem4.tabBarItem.image=[UIImage imageNamed:@"Icon4.png"];
    follow_tab_obj = [[follow_tab alloc] initWithNibName:@"follow_tab" bundle:nil];

    UINavigationController *tabItem5 = [[[UINavigationController alloc] initWithRootViewController: follow_tab_obj] autorelease];
    tabItem5.title=@"Follower";
    tabItem5.tabBarItem.image=[UIImage imageNamed:@"Icon5.png"];


    tabbar1.viewControllers = [NSArray arrayWithObjects:tabItem1, tabItem2,tabItem3,tabItem4,tabItem5,nil]; 

在用戶接受是yes按鈕動作調用此代碼。

[self.view insertSubview:tabbar1.view belowSubview: artist_tab_obj.view];
tabbar1.selectedIndex=1;
[self presentModalViewController:tabbar1 animated:YES]; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM