1

我创建了一个 if 语句,它将在表格编辑时隐藏后退按钮。

但是当离开编辑模式时,我无法让后退按钮重新出现。

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated 
    {
    NSLog(@"Edit");
    bool e = [tableView isEditing];
    e=!e;
    [tableView setEditing:e animated:YES];
    [super setEditing:e animated:YES];

    if (editing)
    {
    [self.navigationItem setHidesBackButton:YES animated:NO];
        UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pushViewController:animated:)];
        self.navigationItem.leftBarButtonItem = add;
    }
    else if (editing == false)
    {
    self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
    //[self.navigationItem setHidesBackButton:NO animated:NO];
    }
}

我在 else 语句中添加了一个 NSLog,它似乎根本没有被调用。

编辑:我解决了它,我简单地将 if(editing == true) 替换为 if(e == true) 它现在可以工作了,不过感谢所有帮助。

4

3 回答 3

0

你需要设置editing == false,或者去掉else if,换成else。目前您正在测试两次以进行编辑 == true

if (editing){
    [self.navigationItem setHidesBackButton:YES animated:NO];
    UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pushViewController:animated:)];
    self.navigationItem.leftBarButtonItem = add;
}else{
    [self.navigationItem setHidesBackButton:NO animated:NO];
}
于 2012-11-06T11:56:54.833 回答
0

您可以将左栏按钮重置为默认的后退按钮,如下所示。

self.navigationItem.leftBarButtonItem   =   self.navigationItem.backBarButtonItem;

编辑:你能试试这样吗?

- (void)setEditing:(BOOL)editing animated:(BOOL)animated 
{
    NSLog(@"Edit");
    bool e = [tableView isEditing];
    e=!e;
   [tableView setEditing:e animated:YES];
   [super setEditing:e animated:YES];

    if (editing)
    {
        UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pushViewController:animated:)];
        self.navigationItem.leftBarButtonItem = add;
    } 
    else
    {
        self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
    }
}

尝试这个。很高兴这对您有帮助。:)

于 2012-11-06T11:59:05.190 回答
0

编辑:我刚刚在一个项目中尝试了以下代码,它按预期工作,隐藏并重新出现 backButton:

- (IBAction)testButton:(id)sender {
    hide = !hide;
    [self.navigationItem setHidesBackButton:hide animated:NO];
}

hide 以 NO 开头。每次我按下 testButton 时,背面都会出现或消失。

您是否在隐藏 backButton 时对 navigationController 或 navigationItem 进行了修改?

于 2012-11-06T12:05:01.083 回答