【问题标题】:how to hide button in other view controller in objective c如何在目标c中隐藏其他视图控制器中的按钮
【发布时间】:2016-06-20 06:49:05
【问题描述】:
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NewsScreen *news=[[NewsScreen alloc] initWithNibName:@"NewsScreen" bundle:nil];

    if (tabBarController.selectedIndex==2) {

        [news.btn setHidden: YES];

    }
    NSLog(@"%@", tabBarController);
}

我想隐藏来自另一个视图控制器的按钮。

【问题讨论】:

  • 有两个问题需要克服。 1)你如何找到对另一个视图控制器的引用并调用它的方法。 2)你如何隐藏一个按钮。其中哪一项给您带来了问题?
  • 2 个。但是当我点击 tabbarcontroller 项目时,我想隐藏其他视图控制器的按钮

标签: ios objective-c xib


【解决方案1】:
    -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

            if (tabBarController.selectedIndex==2) 
            {
              NSUInteger *index=value; //assign value here
              UINavigationController *nv = [[tabBarController viewControllers] objectAtIndex:index];//index of your NewsScreen controller
              NSArray *array =[nv viewControllers];

              for (ViewController *vc in array)
              {
                if ([vc isKindOfClass:[NewScreen class]])
                {
                  [vc.btn setHidden:YES];
                }
              }

            }
            NSLog(@"%@", tabBarController);
        }

【讨论】:

    【解决方案2】:

    将您的代码更改为:

    -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
        NewsScreen *news= (NewsScreen*) [tabBarController.viewControllers objectAtIndex:1]; // Replace 1 with your NewsScree View Controller's index thats your tab number - 1.
    
        if (tabBarController.selectedIndex==2) {
            [news.btn setHidden: YES];
        }
    }
    

    【讨论】:

    • 显示错误无法识别的选择器发送到实例 0x7ff0ea657b00'
    • 您没有提到您在每个选项卡中都使用导航控制器。首先从 tabBarController.viewControllers 中获取导航控制器,然后在该导航控制器的 viewControllers 中检查 NewsScreen 的对象,然后对其执行操作。
    【解决方案3】:

    在您的 ViewController controller 中取一个 BOOL 变量并创建属性。和synthesize 它也是。

    然后这样做:

    ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    vc.check = YES;
    

    viewdidload 写这个:

    if(self.check)
        [mainbutton1 set hidden:YES];
    

    更新

    如果您只想隐藏按钮,请尝试:

    在你的Viewdidload

            [button addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    
    -(void)btnClicked
    {
        [button setHidden:YES]; 
    }
    

    【讨论】:

    • 它没有服用(self.check)
    • 我也不希望它出现在视图控制器中。
    • 如果其他地方发生了某些事情,您希望隐藏某个按钮.... ryt?
    • 或者你只是想要......当你点击按钮......它隐藏了......??
    • 是的,没错。如果不接受(Self.check),甚至直接 btn。它需要 news.btn
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多