xcode 点击 tabBar 时转到 rootView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12142755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Go to rootView when tabBar tapped
提问by piyush
I used view based app & in that i programmatically generate TabBar. Problem is:
我使用了基于视图的应用程序,因为我以编程方式生成 TabBar。问题是:
I have an Iphone application in which i have 2 tabitems with a tabbarcontroller.Inside the tabbarcontroller each viewcontroller is a navigation controller.when selecting the second tab i have a view controller.when selecting a button on that i am pushing another view controller to the self.navigation controller.and in that viewcontroller i am pushing and go like that.But the problem is when i am selecting the tabitem again that pushedviewcotrooller is shown there.but i need that rootview there again when i am selecting the tab
我有一个 Iphone 应用程序,其中我有 2 个带有 tabbarcontroller 的 tabitems。在 tabbarcontroller 中,每个 viewcontroller 都是一个导航控制器。选择第二个选项卡时,我有一个视图控制器。选择一个按钮时,我将另一个视图控制器推到self.navigation controller.and 在那个 viewcontroller 中,我正在推动和那样做。但问题是当我再次选择 tabitem 时,pushviewcotroller 显示在那里。但是当我选择选项卡时,我再次需要那个 rootview
my code in AppDelegate.m is:
我在 AppDelegate.m 中的代码是:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UINavigationController *nc1;
nc1 = [[UINavigationController alloc] init];
UIViewController *viewController1 = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
nc1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
UINavigationController *nc2;
nc2 = [[UINavigationController alloc] init];
UIViewController *viewController2 = [[[secondview alloc] initWithNibName:@"secondview" bundle:nil] autorelease];
nc2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nc1,nc2,nil];
self.window.rootViewController=self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
回答by Maulik
May be you are looking for this :
可能你正在寻找这个:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
int tabitem = tabBarController.selectedIndex;
[[tabBarController.viewControllers objectAtIndex:tabitem] popToRootViewControllerAnimated:YES];
}
回答by Dharmesh Kheni
In swift you can do it this way in your UITabBarController
class:
在 swift 你可以在你的UITabBarController
课堂上这样做:
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController
rootView.popToRootViewControllerAnimated(false)
}
回答by Pandey_Laxman
I believe you will need to employ these two methods:
我相信您将需要使用这两种方法:
UINavigationController
: -popToRootViewControllerAnimated
:
UINavigationController
: - popToRootViewControllerAnimated
:
UITabBarControllerDelegate: tabBarController:didSelectViewController:
The approach I am using in my own program is to only show the tab bar while a root view controller is on the screen.
我在自己的程序中使用的方法是仅在根视图控制器在屏幕上时显示选项卡栏。
回答by Allen
Add UITabBarControllerDelegate
to your AppDelegate
, and in didFinishLaunchingWithOptions
method, set the delegate like UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; tabBarController.delegate = self;
. Then the delegate method - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
will be invoked when tabbar is selected.
添加UITabBarControllerDelegate
到您的AppDelegate
,并在didFinishLaunchingWithOptions
方法中,将委托设置为UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; tabBarController.delegate = self;
。然后在- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
选择选项卡栏时将调用委托方法。