在Xcode 4.5中使用与不同标签视图链接的根视图控制器上使用多个按钮的方法是什么?
我在主屏幕上有4个按钮,我希望当我按下按钮1时,它应该流向tab1,类似于当我点击button2 tab2时应该打开。
我该怎么办?
Dummy Code忽略这个。
{
tabBarController.viewControllers = controllers;
window.rootViewController = tabBarController;
}
答案 0 :(得分:0)
只需创建4个viewControllers并在app delegates方法
中追加代码- (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;
答案 1 :(得分: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];
}
答案 2 :(得分: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;
}
像这样创建一个tabbar控制器
使用AppDelegate对象调用上面的方法,如下面的...
例如:在方法中点击了button1,编写此代码...
- (IBAction)btn1_Clicked:(id)sender{
AppDelegate *objApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
objApp.tabbarcontroller.selectedindex = 2;
}
您可以更改selectedIndex