【问题标题】:Tap twice on selected tab bar item hides the current view controller [duplicate]在选定的标签栏项目上点击两次隐藏当前视图控制器 [重复]
【发布时间】:2013-10-22 12:52:24
【问题描述】:

我在使用导航堆栈推送视图时遇到了一些问题。

我遇到的问题是,在触摸标签栏项目后,视图控制器被推入导航堆栈(来自名为 FirstViewController 的视图控制器),如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    svc = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:svc animated:YES];
}

按预期工作,但实际问题出现在再次触摸相同的标签栏项目时。

当这种情况发生时,当前视图(之前推送的 SecondViewController)被移除,就像我要触摸“完成”按钮一样。

我无法追踪发生这种情况的地点或原因。

编辑:这就是我设置标签栏、视图控制器和导航的方式:

@implementation AppDelegate
@synthesize HomeViewController, FirstViewController, SecondViewController,     ThirdViewController, SettingsViewController, tabBarController, window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    FirstViewController *firstViewController = [[FirstViewController alloc]
                                            initWithNibName:nil bundle:nil];
    UINavigationController *firstViewControllerNav = [[UINavigationController alloc]
                                      initWithRootViewController:firstViewController];

    SecondViewController *secondViewController = [[SecondViewController alloc]
                                             initWithNibName:nil bundle:nil];
    UINavigationController *secondViewControllerNav = [[UINavigationController alloc]
                                                      initWithRootViewController:secondViewController];

    ThirdViewController *thirdViewController = [[ThirdViewController alloc]
                                                  initWithNibName:nil bundle:nil];
    UINavigationController *thirdViewControllerNav = [[UINavigationController alloc]
                                                      initWithRootViewController:thirdViewController];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[firstViewControllerNav,
                                              secondViewControllerNav];

    UITabBar *tabBar = tabBarController.tabBar;

    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];

    [self.window setRootViewController:self.tabBarController];

    [self.window makeKeyAndVisible];

    return YES;
}

【问题讨论】:

  • 这是 TabBarController 的 viewDidLoad 吗?完成按钮在哪里?
  • 请在 tabBarItem 的触摸事件或 tabBarContoller 的触摸委托中分享您的代码,无论您的第一个视图控制器的推送逻辑是否存在?
  • 你好 Kunal,这是 FirstViewController 的 viewDidLoad 一个空的 UIView 类。 (@interface FirstViewController : UIViewController) 另外我在触摸事件中没有任何代码,因为我在 appDelegate didFinishLaunchingWithOptions 中以编程方式添加了所有控件。
  • 那么选择标签栏项目有什么代码吗?

标签: iphone ios objective-c uiviewcontroller pushviewcontroller


【解决方案1】:

触摸标签栏项目两次将导致导航控制器弹回根视图控制器。这是预期的内置行为。

为防止这种情况,您必须使用UITabBarControllerDelegate protocol. 的tabBarController:shouldSelectViewController: 方法

我相信这样的事情可以解决问题:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return viewController != tabBarController.selectedViewController;
}

【讨论】:

  • 好的,有没有办法覆盖这种行为?我应该以不同的方式实施吗?
  • 我认为这种行为适用于双击,这与单击选定的事件不同?
  • 感谢您的回复,为了应用该代码,我应该实现一个自定义标签栏控制器并添加该委托,对吗?
  • @KunalBalani 他提到再次触摸同一个标签栏项目时会出现“问题”。
  • @GastónAlgaze 不,只需在 UITabBarController 上设置委托并实现该方法即可。
猜你喜欢
  • 2019-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-29
  • 2015-12-08
  • 1970-01-01
相关资源
最近更新 更多