在iPhone中的标签栏控制器
我在我的应用程序中添加了两个选项卡,使用这些选项卡加载两个视图控制器
- Tab1:首页
- 表2:collections
所以我写下面的代码来实现这一点
在应用程序代表
AppDelegate.h
@class ViewController,FavViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) ViewController *viewController; @property (strong, nonatomic) FavViewController *favViewController; @property (strong, nonatomic) UITabBarController *tabBarController; @end
AppDelegate.m
@implementation AppDelegate @synthesize window = _window; @synthesize viewController; @synthesize favViewController; @synthesize tabBarController; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; baseTabBarController = [[UITabBarController alloc]init]; baseTabBarController.delegate=self; viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; UINavigationController *homeView = [[UINavigationController alloc]initWithRootViewController:viewController]; favViewController = [[FavViewController alloc] initWithNibName:@"FavViewController" bundle:nil]; favViewController.title = @"My Favourite"; UINavigationController *favouriteView = [[UINavigationController alloc] initWithRootViewController:favViewController]; NSArray *controllers = [NSArray arrayWithObjects:homeView,favouriteView, nil]; baseTabBarController.viewControllers = controllers; [self.window addSubview:baseTabBarController.view]; [self.window makeKeyAndVisible]; return YES; } - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)selectedViewController { if (tabBarController.selectedIndex == 0) { }else if (tabBarController.selectedIndex == 1) { [(FavViewController *)[(UINavigationController*)selectedViewController topViewController] getData]; }else if (tabBarController.selectedIndex == 2) { NSLog(@"2"); } }
这里是我得到的结果屏幕
所以我在这里遇到麻烦
如果我切换到下一个屏幕,我的第一个选项卡不会加载第一个屏幕(主屏幕),而只是停留在当前屏幕上。
让我试一试
在我的应用程序中有四个屏幕让我们说A,B,C,D
我为A和C屏幕添加了选项卡,这些选项卡可以在整个应用程序(全屏)中使用。
现在,如果我将启动应用程序,并进入A – > B-> C – > D并按Home Tab(A),它不会加载屏幕A,而是保持当前屏幕
但好东西是如果我做与另一个选项卡C(我最喜欢)相同的过程加载正确的屏幕。
编辑:我已经实施didSelectViewController方法@sanjitbuild议我,但我不能区分,这个时间点击哪个标签?
我会很感激! 如果有人能指引我正确的方向
使用Tabbar委托并使用uiviewcontroller的参数实例调用poptorootviewcontroller方法。 希望它可以为你工作。 尝试下面的代码
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { UINavigationController *navController = (UINavigationController*)tabBarController.selectedViewController; [navController popToRootViewControllerAnimated:YES]; }
replace代码
[viewController setTabBarItem:tab1]; [favViewController setTabBarItem:tab2];
同
[[tabbarController tabBar] setItems:[NSArray arrayWithObjects:tab1,tab2, nil]];
应该解决。
Viewcontroller.h @property(nonatomic, retain) UITabBarItem *item1; @property(nonatomic, retain) UITabBarItem *item2; @property(nonatomic, retain) UITabBarItem *item3; @property(nonatomic, retain) UITabBarItem *item4; @property(nonatomic, retain) IBOutlet UITabBar *tabBar; Viewcontroller.m @synthesize item1,item2,item3,item4,tabBar; - (void)viewDidLoad { item1 = [[UITabBarItem alloc] initWithTitle:@"Home" image:[UIImage imageNamed:@"home.png"] tag:0]; item2 = [[UITabBarItem alloc] initWithTitle:@"My Lists" image:[UIImage imageNamed:@"mylist"] tag:1]; item3 = [[UITabBarItem alloc] initWithTitle:@"Search" image:[UIImage imageNamed:@"search.png"] tag:2]; item4 = [[UITabBarItem alloc] initWithTitle:@"Settings" image:[UIImage imageNamed:@"setting.png"] tag:3]; NSArray *items = [NSArray arrayWithObjects:item1,item2,item3,item4, nil]; [tabBar setItems:items animated:NO]; [tabBar setSelectedItem:[tabBar.items objectAtIndex:2]]; tabBar.delegate=self; [self.view addSubview:tabBar ]; } /****Function Call******/ - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { if(item.tag == 0) { Newsfeeds *obj = [[Newsfeeds alloc]initWithNibName:@"Newsfeeds" bundle:Nil]; [self.navigationController pushViewController:obj animated:NO]; } else if(item.tag == 1) { MyLists *obj = [[MyLists alloc]initWithNibName:@"MyLists" bundle:Nil]; [self.navigationController pushViewController:obj animated:NO]; } else if(item.tag == 3) { Settings *obj = [[Settings alloc]initWithNibName:@"Settings" bundle:Nil]; [self.navigationController pushViewController:obj animated:NO]; } }
- 在iOS 4.3中将YpCbCr iPhone 4相框渲染为OpenGL ES 2.0纹理
- 在ios应用程序中以纵向模式锁定UIImagePickerController
- 无法使用钥匙串访问(Mac OS X Mavericks)导入代码签名公钥或私钥
- OneSignal不调用didReceiveRemoteNotification
- tableView:numberOfRowsInSection:]:发送到实例的无法识别的选择器
- 旋转后UIWebView内容未调整为新帧
- FAILURE:错误域= NSURLErrorDomain代码= -1004“无法连接到服务器”。
- 围绕MKMapView上的一个点
- ALAsset valueForProperty:ALAssetPropertyLocation未声明?