Question

I'd like to have a UIViewController which has an Add and a Trash button both right to the title, either by adding two UIBarButtonItems to the navigation item/bar or by adding them to a UISegmentedControl and then adding the SegmentedControl to the item. Is this possible? If yes, how is it achieved best?

Was it helpful?

Solution

I have done something similar. I've added two UIButtons to a navigation item/bar by creating a UIView subclass that has two UIButtons in it. You can then do something like this:

MyUIViewSubclass *tempview = [[[MyUIViewSubclass alloc] initWithFrame:CGRectMake(234,4,84,30)] autorelease];
UIBarButtonItem newButton = [[[UIBarButtonItem alloc] initWithCustomView:tempview] autorelease];
[self.navigationItem setRightBarButtonItem:newButton animated:NO];

All you have to do is layout the buttons in MyUIViewSubclass and you're good.

Also, I pass the ID of the target along in a customized init command to make for easier targeting of the buttons in the view. So for MyUIViewSubclass instead of initWithFrame, I have something like this:

- (id)initWithFrame:(CGRect)aRect andTarget:(id)newTarget {
    if (self = [super initWithFrame:aRect]) {



        UIButton *editbtn = [[[UIButton alloc]  initWithFrame:fframe] autorelease];
        [editbtn addTarget:newTarget action:@selector(MBEdit) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:editbtn];
        [self setFirstbutton:editbtn];
        [editbtn release];



        UIButton *newbtn = [[[UIButton alloc]  initWithFrame:fframe] autorelease];
        [newbtn addTarget:newTarget action:@selector(MBNew) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:newbtn];
        [self setSecondbutton:newbtn];
        [newbtn release];

    }

    return self;

}

OTHER TIPS

You can add multiple buttons to a navigation item by wrapping them in a UIToolbar, sample code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top