我什至不确定标题是否正确,但我不明白还能如何解释它,所以就这样吧。我将逐步向您展示我所做的,以便您理解。
首先,我正在使用 UITabBarController,我在其中获得了三个不同的项目。
1) 应用程序启动,它向我展示了第一个 Item,它是一个 UITableView
2) 我点击一个单元格,它会将我推向该单元格的详细 View 。
3) 我从那里导航到另一个 TabBarItem
4) 我导航回第一个 TabBarItem,它仍然显示第 3 步中的 View )。我不希望我希望它向我展示第 1 步的第一个 View )
我怎样才能做到这一点?
最佳答案
您可以实现 UITabBarController
委托(delegate)方法:
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
这会让您知道何时选择了选项卡。
然后,如果您没有对 UIViewController
的引用,则可以访问 UITabBar
的 UIViewController
:
@property(nonatomic, copy) NSArray *viewControllers
从中获取对要重置并调用的适当 UIViewController
(或 UITableViewController
)的引用:
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
它是 UINavigationController
。或者为该调用创建一个公共(public)方法,以这种方式调用 popToRootViewController
。
编辑:
您的 AppDelegate 应如下所示:
@interface AppDelegate () <UITabBarControllerDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.delegate = self;
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController {
NSLog(@"was selecated");
}
关于ios - 如何从 UITabBarItem 获取 RootViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28863744/