0

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

我在主屏幕上有 4 个按钮,我希望流程像我点击 button1 时一样转到 tab1,当我点击 button2 时 tab2 应该打开。

我应该怎么办?

虚拟代码忽略这个。

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

}
4

3 回答 3

0

只需创建 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;
于 2013-07-10T09:03:22.517 回答
0

你可以像下面那样做..

UITabBarController首先在AppDelegate类中创建对象并在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];
}
于 2013-07-10T09:03:33.267 回答
0
    - (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

于 2013-07-10T09:49:01.873 回答