【问题标题】:RootViewController to TabBarViewControllerRootViewController 到 TabBarViewController
【发布时间】:2013-07-10 08:52:58
【问题描述】:

在 Xcode 4.5 中使用与不同选项卡视图链接的根视图控制器上的多个按钮的方法是什么?

我在主屏幕上有 4 个按钮,我希望当我点击一个按钮 1 时,它应该转到 tab1,类似地,当我点击 button2 时,tab2 应该打开。

我该怎么办?

虚拟代码忽略这个。

{
   tabBarController.viewControllers = controllers;
    window.rootViewController = tabBarController;

}

【问题讨论】:

    标签: iphone ios uitabbarcontroller xcode4.5


    【解决方案1】:

    你可以像下面这样..

    首先在AppDelegate 类中创建UITabBarController 的对象并在applicationDidFinishLaunching: 方法中分配,如下所示..

    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController2 = [[[UITabBarController alloc]init]autorelease];
    self.tabBarController3 = [[[UITabBarController alloc]init]autorelease];
    

    在创建方法后将不同的选项卡设置为 RootViewController,如下所示...

    -(void)setRootViewControllerTab1{
    
        UIViewController *viewController1, *viewController2;
        UINavigationController *navviewController1 , *navviewController2,;
    
        viewController1 = [[[HomeViewController alloc] initWithNibName:@"viewController1" bundle:nil] autorelease];
        navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
        navviewController1.title = @"Title1";
    
    
        viewController2 = [[[HowItWorksViewController alloc] initWithNibName:@"viewController2" bundle:nil] autorelease];
        navviewController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
        navviewController2.title = @"Title2";
        self.tabBarController.viewControllers = [NSArray arrayWithObjects:navviewController1, navviewController2, nil];
    
        self.window.rootViewController = self.tabBarController;
    
        [self.window makeKeyAndVisible];
    }
    -(void)setRootViewControllerTab2{
    
        UIViewController *viewController3, *viewController4;
        UINavigationController *navviewController3 , *navviewController4;
        /////TAB 2/////***********
        viewController3 = [[[CUHomeViewController alloc] initWithNibName:@"viewController3" bundle:nil] autorelease];
        navviewController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
        navviewController3.title = @"Title3";
    
        viewController4 = [[[CUFavouritiesViewController alloc] initWithNibName:@"viewController4" bundle:nil] autorelease];
        navviewController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
        navviewController4.title = @"Title4";
        self.tabBarController2.viewControllers = [NSArray arrayWithObjects:navviewController3, navviewController4, nil];
    
        self.window.rootViewController = self.tabBarController2;
    
        [self.window makeKeyAndVisible];
    }
    

    使用AppDelegate 对象调用上述方法,如下所示...

    例如:按钮1当时在方法中点击了写这段代码...

    - (IBAction)btn1_Clicked:(id)sender{
            AppDelegate *objApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            [objApp setRootViewControllerTab1];
    }
    

    【讨论】:

      【解决方案2】:
          - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      
      {
       UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease]; 
          UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease]; 
          UIViewController *viewController3 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease]; 
          UIViewController *viewController4 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease]; 
          self.tabBarController = [[[UITabBarController alloc] init] autorelease]; self.tabBarController.viewControllers = @[viewController1, viewController2,viewController3,viewController4]; 
          self.window.rootViewController = self.tabBarController;
      }
      

      像这样创建一个标签栏控制器

      使用 AppDelegate 对象调用上述方法,如下所示...

      例如:按钮1当时在方法中单击,请编写此代码...

      - (IBAction)btn1_Clicked:(id)sender{
          AppDelegate *objApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
          objApp.tabbarcontroller.selectedindex = 2;
      }
      

      你可以改变 selectedIndex

      【讨论】:

        【解决方案3】:

        只需创建 4 个 viewController 并在应用程序委托方法中附加以下代码

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
            UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
            UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
            UIViewController *viewController3 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
            UIViewController *viewController4 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
            self.tabBarController = [[[UITabBarController alloc] init] autorelease];
            self.tabBarController.viewControllers = @[viewController1, viewController2,viewController3,viewController4];
            self.window.rootViewController = self.tabBarController;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多