【问题标题】:UITabBarControllerDelegate method not getting called未调用 UITabBarControllerDelegate 方法
【发布时间】:2015-10-15 04:46:03
【问题描述】:

我有一个标签栏控制器,它有 4 个标签,我想要:当点击第 4 个标签(一个虚拟视图控制器)时,它将呈现一个新的视图控制器而不显示虚拟 VC。

这是我的代码:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSLog(@"called");
    AskQuestionViewController *AQVC = [[AskQuestionViewController alloc]initWithNibName:@"AskQuestionViewController" bundle:nil];
    if (viewController == [tabBarController.viewControllers objectAtIndex:3])
    {
        [self presentViewController:AQVC animated:YES completion:nil];
        return NO;
    }
    return YES;
}

在我的 viewDidLoad 方法中,我确实设置了委托。self.tabBarController.delegate = self;

但是,由于某种原因,没有调用此方法。 谁能帮忙?

【问题讨论】:

  • 您是否在 delgate=self 行收到任何警告?
  • @T_77 不,我没有。在我的 .h 文件中,我确实添加了 UITabaBarControllerDelegate。

标签: ios objective-c iphone uitabbarcontroller


【解决方案1】:

因为这个类是一个tabBarController,显然UITabBarController 类没有一个名为tabBarController 的属性。 所以我只是将self.tabBarController.delegate = self 设置为self.delegate = self

【讨论】:

  • 这行得通,但我不知道这是不是一个好的解决方案?
  • @Gellert 你是我的英雄 :-)
【解决方案2】:

你需要使用:

- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController

告诉代理用户在标签栏中选择了一个项目。

- tabBarController:shouldSelectViewController:

询问委托是否应该激活指定的视图控制器。

【讨论】:

  • 无论如何你需要告诉代理你点击了第四个标签,你可以在点击第四个标签后调用视图控制器。试试这个,如果发生任何问题,请告诉我
  • 我把 self.tabBarController.delegate = self 改成了 self.delegate = self.
  • 因为这个类是tabBarController,显然UITabBarController类没有tabBarController的属性。
  • 你在哪里调用这个委托方法?
  • 您需要在 viewDidLoad() 中分配委托,如果您的类正在实现 UITabBarController,请执行 self.delegate = self 而不是 self.tabController?.delegate = self
【解决方案3】:

任何对UITabBarController 有问题的人都使用Hero 代表。问题在于Hero 转换委托,解决方案是将您的tabBarController 作为container 放在ViewController 中。然后在容器上设置Hero,问题就解决了。

tabBarController.viewControllers = [...]
let container = UIViewController()
container.addChild(tabBarController)
container.view.addSubview(tabBarController.view)
tabBarController.didMove(toParent: container)
tabBarController.view.frame = CGRect(x: 0, y: 0, width: container.view.frame.width, height: container.view.frame.height)
container.modalPresentationStyle = .fullScreen
container.hero.isEnabled = true
container.modalAnimationType = .slide(direction: .left)
rootViewController.present(container, animated: true, completion: nil)

【讨论】:

    猜你喜欢
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2019-09-01
    相关资源
    最近更新 更多