在UITabBar中更改ViewController时,不会删除IOS UiBarButtoItemn

时间:2015-05-06 22:29:39

标签: ios uinavigationcontroller uibarbuttonitem uitabbar

我以编程方式从UIButton创建了2个UIBarButtonItem。导航控制器位于UITabBar中。当我移动到同一个UITabBar中的另一个控制器时,这两个UIBarButton不会被移除。任何人都知道如何删除这两个UIBarButtonItem

- (void)viewWillAppear:(BOOL)animated {

    // Left Tab Bar Items
    UIButton *fbutton = [[UIButton alloc] init];
    [fbutton setImage:[[UIImage imageNamed:@"ble"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [fbutton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    fbutton.frame=CGRectMake(0, 0, 48  , 48);
    UIBarButtonItem *fBLEItem = [[UIBarButtonItem alloc] initWithCustomView:fbutton];
    fBLEItem.enabled = NO;

    UIButton *tbutton = [[UIButton alloc] init];
    [tbutton setImage:[[UIImage imageNamed:@"ble"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [tbutton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    tbutton.frame=CGRectMake(0, 0, 48, 48);
    UIBarButtonItem *tBLEItem = [[UIBarButtonItem alloc] initWithCustomView:tbutton];
    tBLEItem.enabled = NO;

    self.tabBarController.navigationItem.leftItemsSupplementBackButton = YES;
    self.tabBarController.navigationItem.leftBarButtonItems = @[fBLEItem,tBLEItem];

}


- (void) viewWillDisappear:(BOOL)animated {

    [self.navigationItem setLeftBarButtonItem:nil animated:NO];
    [self.navigationItem setRightBarButtonItem:nil animated:NO];

}

1 个答案:

答案 0 :(得分:0)

您在UIBarButtonItem上设置self.tabBarController.navigationItem,但在nil上设置self.navigationItem。这是两件不同的事情。尝试:

[self.tabBarController.navigationItem setLeftBarButtonItem:nil animated:NO]; 
[self.tabBarController.navigationItem setRightBarButtonItem:nil animated:NO];