How to change a UIBarButtonItem in a UINavigationBar

前端 未结 5 1742
醉酒成梦
醉酒成梦 2021-02-06 04:40

I\'m trying to set up a list of items that can be edited. I have a main view, with a UINavigationBar at the top and a UITableView directly under it. I\'d like to have my \"edit\

相关标签:
5条回答
  • 2021-02-06 05:07

    Got it! Looks like you can get to the UINavigationItem by using the topItem property. Just had to read through the docs, like always!

    0 讨论(0)
  • 2021-02-06 05:08

    set up nav button in IB with the following linked in IB and create an outlet called editOutlet and and an action called editToggle in your header file and your method is as easy as this:

    -(IBAction) editToggle:(id) sender {
    
    if (self.tableViewOutlet.isEditing == NO) {
    
        self.editOutlet.title = NSLocalizedString(@"Done", @"Done");
        self.editOutlet.style = UIBarButtonItemStyleDone;
        [self.tableViewOutlet setEditing:YES animated:YES];
    
    }else {
        self.editOutlet.title = NSLocalizedString(@"Edit", @"Edit"); 
        self.editOutlet.style = UIBarButtonItemStylePlain;
        [self.tableViewOutlet setEditing:NO animated:YES];
    
        }
    }
    
    0 讨论(0)
  • 2021-02-06 05:14

    you can try it

    [self setValue:viewController.navigationItem forKey:@"_navigationItem"];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
    0 讨论(0)
  • 2021-02-06 05:32

    When you use self.editButtonItem, you don't need to change the style and text of the button, it is done automatically. Try removing that code, it will still work :)

    0 讨论(0)
  • 2021-02-06 05:33

    I create one button that can change from Edit to Done. It's a tip from More iPhone Development book.

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIBarButtonItem *editButton = self.editButtonItem;  
        [editButton setTarget:self]; 
        [editButton setAction:@selector(toggleEdit)]; 
        self.navigationItem.leftBarButtonItem = editButton; 
    }
    

    And the method toggleEdit

    - (IBAction)toggleEdit { 
        BOOL editing = !self.tableView.editing; 
        self.navigationItem.rightBarButtonItem.enabled = !editing; 
        if (editing) {
            self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"Done", @"Done");
            //Added in the edition for this button has the same color of the UIBarButtonSystemItemDone
            self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;
        } else {
            self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"Edit", @"Edit"); 
            //Added in the edition for this button has the same color of the UIBarButtonSystemItemDone
            self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStylePlain;
        }
        [self.tableView setEditing:editing animated:YES]; 
    } 
    

    Then you don't need replace any of them.

    0 讨论(0)
提交回复
热议问题