在 iPhone 中推送 UIViewController 时如何更改标签栏项目的选定索引
Posted
技术标签:
【中文标题】在 iPhone 中推送 UIViewController 时如何更改标签栏项目的选定索引【英文标题】:how to change selected index of tabbar item when pushing UIViewController in iPhone 【发布时间】:2013-01-08 03:55:56 【问题描述】:我正在将一个视图控制器推送到另一个视图控制器,但是当我推送标签栏项目时索引没有改变。如何在将视图从一个推送到另一个时更改选定的索引。提前致谢。
【问题讨论】:
标签栏项目索引?..你的意思是标签控制器选择的索引对吗? 请澄清一下这个问题还有什么含糊不清的。需要更多细节才能给出正确答案。 好好阅读本文档developer.apple.com/library/ios/#documentation/UIKit/Reference/… 你有多少个视图控制器?超过 6 个? 当我将视图控制器从 tabbarcontroller 的 0 索引推送到另一个具有 1 索引的视图控制器时。从 0 推到 1 后,我想将 selectedIndex 显示为 1。 - Wolvorin 【参考方案1】:试试,
[tabBar setSelectedItem:[[tabBar items] objectAtIndex:index]];
或
[tabBarController setSelectedIndex:index];
【讨论】:
【参考方案2】:这条线可能对某人有帮助
[self.parentViewController.tabBarController setSelectedIndex:0];
【讨论】:
这对我有帮助 :) 谢谢【参考方案3】: [self.tabBarController setSelectedIndex:0];
这里的索引 0 是指标签栏控制器的第一个标签。
【讨论】:
我在我的应用程序中使用 Three20【参考方案4】:UITabBarController
的简单示例 for You..仅创建三个 UIViewController
。此代码工作正常 :)
appDelegate.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import "ViewController.h"
#import "SecoundViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate,UINavigationControllerDelegate>
FirstViewController *fView;
SecoundViewController *sView;
ViewController *viewCon;
UITabBarItem *tbItem1;
UITabBarItem *tbItem2;
UITabBarItem *tbItem3;
UINavigationController *FnavCon;
UINavigationController *SnavCon;
UINavigationController *navCon;
UITabBarController *tbCon;
@property (strong, nonatomic) UIWindow *window;
@property(nonatomic,retain)FirstViewController *fView;
@property(nonatomic,retain)SecoundViewController *sView;
@property(nonatomic,retain)UINavigationController *navCon;
@property(nonatomic,retain)UITabBarController *tbCon;
@property(nonatomic,retain)ViewController *viewCon;
@property(nonatomic,retain)UINavigationController *FnavCon;
@property(nonatomic,retain)UINavigationController *SnavCon;
@property(nonatomic,retain)UITabBarItem *tbItem1;
@property(nonatomic,retain)UITabBarItem *tbItem2;
@property(nonatomic,retain)UITabBarItem *tbItem3;
@end
appDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize fView,sView,navCon,tbCon,viewCon,FnavCon,SnavCon,tbItem1,tbItem2,tbItem3;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds ]];
self.viewCon=[[ViewController alloc] init];
self.navCon=[[UINavigationController alloc] initWithRootViewController:self.viewCon];
self.navCon.navigationBar.tintColor=[UIColor blackColor];
self.viewCon.title=@"First View";
self.fView=[[FirstViewController alloc] init];
self.FnavCon=[[UINavigationController alloc] initWithRootViewController:self.fView];
self.FnavCon.navigationBar.tintColor=[UIColor blackColor];
self.fView.title=@"Secound View";
self.sView=[[SecoundViewController alloc] init];
self.SnavCon=[[UINavigationController alloc] initWithRootViewController:self.sView];
self.SnavCon.navigationBar.tintColor=[UIColor blackColor];
self.sView.title=@"Third View";
UIImage *img1=[UIImage imageNamed:@"Australia.gif"];
self.tbItem1=[[UITabBarItem alloc] initWithTitle:@"First Page" image:img1 tag:1];
self.viewCon.tabBarItem=self.tbItem1;
UIImage *img2=[UIImage imageNamed:@"Cameroon.gif"];
self.tbItem2=[[UITabBarItem alloc] initWithTitle:@"Secound Page" image:img2 tag:2];
self.fView.tabBarItem=self.tbItem2;
UIImage *img3=[UIImage imageNamed:@"Canada.png"];
self.tbItem3=[[UITabBarItem alloc] initWithTitle:@"Third Page" image:img3 tag:3];
self.sView.tabBarItem=self.tbItem3;
NSMutableArray *viewArr=[[NSMutableArray alloc] init];
[viewArr addObject:self.navCon];
[viewArr addObject:self.FnavCon];
[viewArr addObject:self.SnavCon];
self.tbCon=[[UITabBarController alloc] init];
self.tbCon.viewControllers=viewArr;
[self.window addSubview:tbCon.view];
[self.window makeKeyAndVisible];
return YES;
【讨论】:
以上是关于在 iPhone 中推送 UIViewController 时如何更改标签栏项目的选定索引的主要内容,如果未能解决你的问题,请参考以下文章