首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RootViewController到TabBarViewController

RootViewController到TabBarViewController
EN

Stack Overflow用户
提问于 2013-07-10 16:52:58
回答 3查看 1K关注 0票数 0

在Xcode4.5中,如何使用与不同选项卡视图链接的根视图控制器上的多个按钮?

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

我该怎么办?

伪代码忽略这一点。

代码语言:javascript
复制
{
   tabBarController.viewControllers = controllers;
    window.rootViewController = tabBarController;

}
EN

回答 3

Stack Overflow用户

发布于 2013-07-10 17:03:23

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

代码语言:javascript
复制
- (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;
票数 0
EN

Stack Overflow用户

发布于 2013-07-10 17:03:33

你可以像贝洛那样做..

首先在AppDelegate类中创建UITabBarController对象,然后在applicationDidFinishLaunching:方法中分配对象,如下所示。

代码语言:javascript
复制
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController2 = [[[UITabBarController alloc]init]autorelease];
self.tabBarController3 = [[[UITabBarController alloc]init]autorelease];

在创建方法将不同的选项卡设置为类似于下面的RootViewController ...

代码语言:javascript
复制
-(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对象调用上面的方法...

例如: button1在当时的方法中点击了写这段代码...

代码语言:javascript
复制
- (IBAction)btn1_Clicked:(id)sender{
        AppDelegate *objApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [objApp setRootViewControllerTab1];
}
票数 0
EN

Stack Overflow用户

发布于 2013-07-10 17:49:02

代码语言:javascript
复制
    - (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;
}

创建一个tabbar控制器,如下所示

像下面这样用AppDelegate对象调用上面的方法...

例如: button1在当时的方法中单击了编写此代码...

代码语言:javascript
复制
- (IBAction)btn1_Clicked:(id)sender{
    AppDelegate *objApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    objApp.tabbarcontroller.selectedindex = 2;
}

您可以更改selectedIndex

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17566280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档