Friday, May 31, 2013

UIBarButtonItem UIBarButtonSystemItemDone example in Objective C (iOS).


UIBarButtonItem UIBarButtonSystemItemDone


UIBarButtonSystemItem
Defines system-supplied images for bar button items.

typedef enum {
UIBarButtonSystemItemDone,
UIBarButtonSystemItemCancel,
UIBarButtonSystemItemEdit,
UIBarButtonSystemItemSave,
UIBarButtonSystemItemAdd,
UIBarButtonSystemItemFlexibleSpace,
UIBarButtonSystemItemFixedSpace,
UIBarButtonSystemItemCompose,
UIBarButtonSystemItemReply,
UIBarButtonSystemItemAction,
UIBarButtonSystemItemOrganize,
UIBarButtonSystemItemBookmarks,
UIBarButtonSystemItemSearch,
UIBarButtonSystemItemRefresh,
UIBarButtonSystemItemStop,
UIBarButtonSystemItemCamera,
UIBarButtonSystemItemTrash,
UIBarButtonSystemItemPlay,
UIBarButtonSystemItemPause,
UIBarButtonSystemItemRewind,
UIBarButtonSystemItemFastForward,
UIBarButtonSystemItemUndo, // iOS 3.0 and later
UIBarButtonSystemItemRedo, // iOS 3.0 and later
UIBarButtonSystemItemPageCurl, // iOS 4.0 and later
} UIBarButtonSystemItem;

UIBarButtonItem UIBarButtonSystemItemDone example.
- (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];

Example of [UIBarButtonItem UIBarButtonSystemItemDone].
- (void)textFieldDidBeginEditing:(UITextField *)textField
{      
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                target:self
                                                                                action:@selector(doneEditing)];
    [[self navigationItem] setRightBarButtonItem:doneButton];
    [doneButton release];
}

UIBarButtonItem UIBarButtonSystemItemDone example.
- (void)textViewDidBeginEditing:(UITextView *)textView {
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                target:textView
                                                                                action:@selector(resignFirstResponder)];
    self.navigationItem.rightBarButtonItem = doneButton;
    [doneButton release];
}

End of UIBarButtonItem UIBarButtonSystemItemDone example article.