Creating Edit and Save/Done buttons in navigation bar programmatically

This took annoyingly long time to figure out. There isn’t much info on the web about it surprisingly. Table view comes with Edit/Done buttons automatically and most info out there is about table views. I had my own view with text field controls and such. I wanted to have an Edit button. When pressed, it changes to Save with blue background and displays edit controls that are normally not there. Here goes,

- (void)viewDidLoad {

  [super viewDidLoad];

  mBarButtonItemEdit = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editButtonPressed)];

  mBarbuttonitemSave = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(editButtonPressed)];

  self.navigationItem.rightBarButtonItem = mBarButtonItemEdit;
}

I used a boolean inside the function editButtonPressed to know the state (Edit or Save state). Nice and easy.

Posted in: Objective C, Uncategorized, iPhone



addLeave a comment