Ios 从子视图控制器隐藏UIAbbarController中添加的UIButton

Ios 从子视图控制器隐藏UIAbbarController中添加的UIButton,ios,objective-c,xcode,uibutton,uitabbarcontroller,Ios,Objective C,Xcode,Uibutton,Uitabbarcontroller,我有一个TabBar应用程序,我还添加了一个UIButton,它会打开一个菜单 以下是将菜单按钮添加到选项卡控制器的方式: //TabBarViewController.m - (void)viewDidLoad { [super viewDidLoad]; self.menuBtn = [UIButton buttonWithType:UIButtonTypeCustom]; menuBtn.frame = CGRectMake(285, 28, 24, 24);

我有一个TabBar应用程序,我还添加了一个UIButton,它会打开一个菜单

以下是将菜单按钮添加到选项卡控制器的方式:

//TabBarViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(285, 28, 24, 24);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.menuBtn];
}
此功能正常,并产生以下结果:

我在选项卡栏控制器中有一个按钮,因此它显示在每个选项卡上

这一切都很好,但当我使用搜索栏时,菜单按钮会变成:

在搜索处于活动状态时,如何隐藏菜单按钮? 我已经找到了如何检测搜索栏何时处于活动状态的方法,但是我在试图隐藏菜单按钮时遇到了问题

//SearchViewController.m

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    TabBarViewController *vc = [[TabBarViewController alloc]init];
    [vc.menuBtn setHidden:YES];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {   
    TabBarViewController *vc = [[TabBarViewController alloc]init];
    [vc.menuBtn setHidden:NO];
}
我尝试了一些方法,但没有什么能隐藏菜单按钮。这是不可能的,还是我错过了什么


如有任何帮助,我们将不胜感激,感谢rdelmar的评论:

您需要获取对已经拥有的选项卡栏控制器的引用,而不是使用alloc init创建一个新的

目前,我正在制作一个新的TabBarViewController,其中包括:

TabBarViewController *vc = [[TabBarViewController alloc]init];
相反,我需要使用一个参考。通过使用“parentViewController”,我可以做到这一点

TabBarViewController中的应用程序结构为:

  • TabBarViewController
  • 搜索导航控制器
  • SearchViewController
  • 因此,通过两次使用“parentViewController”,我能够引用TabBarViewController,然后隐藏菜单项

    以下是对我有效的方法:

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
        ((TabBarViewController *)self.parentViewController.parentViewController).menuBtn.hidden = YES;
    }
    
    - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
        ((TabBarViewController *)self.parentViewController.parentViewController).menuBtn.hidden = NO;
    }
    

    这当然是可能的,但您需要获得对已经拥有的选项卡栏控制器的引用,而不是使用alloc init创建一个新的。如果不了解更多关于应用程序结构的信息,我无法告诉您如何做到这一点。你添加按钮的那个条是什么?你们有什么控制器?TabBarViewController是初始视图控制器吗?@rdelmar我理解你的意思,有道理。但仍然不确定如何创建引用。TabBarViewController不是初始视图控制器,我有一个UIViewController作为init(menuBtn连接到它)。结构如下:
    MenuViewController(UIViewController)
    =>
    TabBarViewController(UITabBarController)
    =>
    SearchNavigationController(UINavigationController)
    =>
    SearchViewController(UITableViewController)
    所以
    menuBtn
    选项卡BarViewController
    中,我需要对
    SearchViewController
    隐藏它,那么您添加按钮的栏是什么?这是SearchNavigationController的导航栏,还是向选项卡栏控制器本身添加了导航栏?按钮添加到选项卡栏控制器本身的
    TabBarViewController
    ,TabBarViewController没有导航控制器。单击父视图控制器选项卡栏按钮时如何隐藏TabView?