tags:

views:

405

answers:

2

hi i want to include segmented control in navigation item with flexible space.bcos it has 2 control .. i want to bring in the center....i tried follow .. will u help ? ...the line...

self.navigationItem.rightBarButtonItem = [NSArray   arrayWithObjects:segmentBarItem,flexibleSpaceButtonItem,nil];

gives error?

 UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] 
 initWithCustomView:segmentedControl];

[segmentedControl release];

UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc]
 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];

self.navigationItem.rightBarButtonItem = [NSArray arrayWithObjects:segmentBarItem,flexibleSpaceButtonItem,nil];
A: 

This isn't going to work; rightBarButtonItem is of type UIBarButtonItem *, and you're trying to stuff an NSArray * into it. As far as I know, there's no standard way to put an extra button into the navigation bar.

David Maymudes
+2  A: 

I think the objective of ur problem is to add more than one button to the right bar button item?

If it is you can do it in this way......

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 45)];
        [tools setTintColor:[self.navigationController.navigationBar tintColor]];
        NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];

        UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc]
        initWithCustomView:segmentedControl];
        [segmentedControl release];

        UIBarButtonItem *flexibleSpaceButtonItem =[[UIBarButtonItem alloc]
        initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil 
        action:nil];

        [buttons addObject:flexibleSpaceButtonItem];
        [flexibleSpaceButtonItem release];

        [buttons addObject:segmentBarItem];
        [segmentBarItem release];



        [tools setItems:buttons animated:NO];

        [buttons release];

        UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithCustomView:tools];

        self.navigationItem.rightBarButtonItem = myBtn;

        [myBtn release];
        [tools release];

Don't forget to vote if this solves ur problem .... ;)

Madhup