I’m using the following code to create a custom edit button item:
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
[editButton addTarget:self action:@selector(startEditing) forControlEvents:UIControlEventTouchUpInside];
[editButton setTitle:@"Filters" forState:UIControlStateNormal];
editButton.frame = CGRectMake(editButton.frame.origin.x, editButton.frame.origin.y, 60.0, 30.0);
[editButton setBackgroundImage:[UIImage imageNamed:@"contact_btn_bg.png"] forState:UIControlStateNormal];
[editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[editButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12.0f]];
UIBarButtonItem *cEditButtonItem = [[UIBarButtonItem alloc] initWithCustomView:editButton];
self.navigationItem.rightBarButtonItem = cEditButtonItem;
[editButton release];
But I am not sure how to customize the “Done” state. Of course I can use an if/then switch to call a new (void)finishEditing procedure but I’m not sure what would need to be in the finish editing proc, nor am I sure if I should be incorporating the idea of states in this or just switching out the button for Normal State when I need to.
My (void)startEditing looks like this:
- (void)startEditing {
[self setEditing:YES animated:YES];
}
Here’s how I handle this in my apps.
First, create two properties on your class for your two
UIBarButtonItems.Make sure that they’re synthesized and configure them with the appropriate target/action in
-viewDidLoad. Now create a method that returns aUIBarButtonItem.Now in the methods you’ve assigned to your Edit and Done buttons, you just need to set the appropriate button in your
UINavigationItemto the method we created.