更改在tabbar应用程序中单击的按钮上的视图

时间:2012-09-19 11:10:06

标签: iphone objective-c ios xcode uitabbarcontroller

在我的基于标签的应用中,我使用4个标签以编程方式创建了标签栏应用。在我的第一个标签中有5个scrren&在第3个标签中有3个屏幕。

在我的第一个标签的第二个屏幕上,当我点击我想要的一个按钮时,它应该转到第三个标签中的第二个屏幕。但是现在它转到第三个标签上最后打开的屏幕。

e.g。 tabControllers:a,b,c,d

a tab: 1->2>3>4>5

C  tab:  1>2>3

我希望选中的标签是:&屏幕是:3&

点击该屏幕上的按钮

选中的标签是C&屏幕是第二个

参考链接是: Change the selected TabBar index on button click

我的代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 //for home tab..
    UINavigationController *nav1 = [[UINavigationController alloc] init];
    UIViewController *viewController1 = [[[CarAccidentAppViewController alloc] initWithNibName:@"CarAccidentAppViewController_iPhone" bundle:nil] autorelease];
    nav1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];

    //for steps tab...
    UINavigationController *nav2 = [[UINavigationController alloc] init];
    UIViewController *viewController2 = [[[FirstSteps alloc] initWithNibName:@"FirstSteps" bundle:nil] autorelease];
    nav2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];

    //for profiles tab...
    UINavigationController *nav3 = [[UINavigationController alloc] init];
    UIViewController *viewController3 = [[[Profiles alloc] initWithNibName:@"Profiles" bundle:nil] autorelease];
    nav3.viewControllers = [NSArray arrayWithObjects:viewController3, nil];

    //for contact us tab...
    UINavigationController *nav4 = [[UINavigationController alloc] init];
    UIViewController *viewController4 = [[[ContactUs alloc] initWithNibName:@"ContactUs" bundle:nil] autorelease];
    nav4.viewControllers = [NSArray arrayWithObjects:viewController4, nil];


    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nav4 ,nil];

    self.window.rootViewController=self.tabBarController;
      [self.window makeKeyAndVisible];
    return YES;

}

//Button click event 
-(IBAction)btnNewDriverPressed:(id)sender
{
    [[NSUserDefaults standardUserDefaults]setInteger:0 forKey:@"showselectedIndex"];
    self.tabBarController.selectedViewController=[self.tabBarController.viewControllers 
objectAtIndex:2];
}

2 个答案:

答案 0 :(得分:0)

请上传您的流量图片,说明您实际想要实现的目标。

使用viewcontroller方法浏览您的愿望screen / initwithnibname

关注:

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[self.navigationController pushViewController:self.viewController animated:YES];

答案 1 :(得分:0)

很抱歉你无法正常工作..如果你在第一个屏幕的“C”标签中,你只需点击任意按钮即可转到第二个屏幕。通过按c选项卡,它不会将第二个视图控制器加载到导航控制器而无需按任何按钮。所以你需要自己推到第二个屏幕并选择c选项卡..但是从我看来你的应用程序设计并不好,如果你喜欢它。它不会带来最佳的用户体验。

相关问题