Tuesday, June 11, 2013

UIToolbar items example in Objective C (iOS).

UIToolbar items

The items displayed on the toolbar.

@property(nonatomic, copy) NSArray *items

Discussion of [UIToolbar items]
The items, instances of UIBarButtonItem, that are visible on the toolbar in the order they appear in this array. Any changes to this property are not animated. Use the setItems:animated: method to animate changes.

The default value is nil.

UIToolbar items example.
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, settingsButton,deleteButton,aboutButton, flexibleSpace, nil]];
[flexibleSpace release];

Example of [UIToolbar items].
- (void)updateBarButtonItemChecked:(BOOL)checked {
    if (toolbarItems == nil) {
     toolbarItems = [[NSMutableArray arrayWithArray:toolbar.items] retain];
     checkUncheckIndex = -1;

     for (NSUInteger i = 0; i < [toolbarItems count]; i++) {
     UIBarButtonItem *barButtonItem = [toolbarItems objectAtIndex:i];
     if (barButtonItem.action == @selector(checkUncheckClicked)) {
     favoriteIndex = i;
     break;
     }
     }
    }

    if (checkUncheckIndex != -1) {
     UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:checked ? @"checked.png" : @"unchecked.png"]
        style:UIBarButtonItemStylePlain target:self action:@selector(checkUncheckClicked)] autorelease];
     [toolbarItems replaceObjectAtIndex:checkUncheckIndex withObject:barButtonItem];

     toolbar.items = toolbarItems;
    }
}

UIToolbar items example.
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 45);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithObjects...]];
[toolbar setItems:items animated:NO];
[self.view addSubview:toolbar];

End of UIToolbar items example article.