How to change a UIBarButtonItem in a UINavigationBar

前端 未结 5 1745
醉酒成梦
醉酒成梦 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: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.

提交回复
热议问题