如何在目标c中隐藏其他视图控制器中的按钮

时间:2016-06-20 06:49:05

标签: ios objective-c xib

-(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);
}

我想隐藏来自其他视图控制器的按钮。

3 个答案:

答案 0 :(得分:2)

    -(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);
        }

答案 1 :(得分:0)

BOOL ViewController中获取controller变量并制作该属性。和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]; 
}

答案 2 :(得分:0)

将您的代码更改为:

-(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];
    }
}