Put self.editbuttonitem into segmentedcontrol as barbuttonitem? - iphone

I want to make a NavBar similar to the one in sample 3 of the NavBar sample code, except I want to use the self.editbuttonItem as one of the two buttons in the SegmentedControl. (The other will be a custom add button.) Basically - the end result will be a leftBarButtonItem that's just one button, bringing up a modal view, and a rightBarButtonItem that's a segmented control with both edit and add buttons.
Thing is, it looks like setting up the SegmentedControl needs an array of Strings or Images, but not BarButtonItems. Is there a workaround?
This is the relevant bit from Apple's sample:
// "Segmented" control to the right
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
Instead of the images, I want to put BarButtonItems...

If self.editbuttonitem is a UIBarButtonItem with title "Edit", I think you can do with the following code
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
#"Edit", #"AnotherButtonName"
nil]];
edit based on Charles Bandes's comment
Add an action to the segmentedControl, like the Apple's sample:
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//...
then in segmentAction:, do
- (void)segmentAction:(UISegmentedControl*)sender
{
//if the "edit" item in segmentedControl is selected
if (sender.selectedSegmentIndex == 0)
{
//I assume self is a UITableView instance
//start editing
[self setEditing:YES animated:YES];
}
}
I wrote those code on my PC. However it should work.
You may take a look at [UITableView setEditing:animated:]

Related

Setting segmented control at the centre of the tab

I am working with the segmented control. I am using the following code to set it at the left of the tab.
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
segmentedControl.segmentedControlStyle =
UISegmentedControlStyleBezeled;
segmentedControl.frame = CGRectMake(100,10,220,35);
[segmentedControl insertSegmentWithTitle:#"Male" atIndex:0
animated:YES];
[segmentedControl insertSegmentWithTitle:#"Female" atIndex:1
animated:YES];
segmentedControl.selectedSegmentIndex = 1;
[segmentedControl setMomentary:NO];
[segmentedControl addTarget:self action:#selector(segmentSwitch:)
forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc]
initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.leftBarButtonItem = segmentBarItem;
[segmentBarItem release];
My question is how can we set it at the center of the tab bar, as it is not taking the CGRect() values to set the position.
I think you are trying to add the UISegmentedControl to UINavigationBar not UITabBar. If you want it at the center you can set it as the titleView of navigationItem.
self.navigationItem.titleView = segmentedControl;

tintColor of UISegmentedControl doesnt react

Come oon!
I am almost looking a day at this!
I want to set my tint color of my UISegmentedControl but it doesnt react...
NSArray *segControlItems = [NSArray arrayWithObjects:
#"L",
#"H",
#"A",
nil];
UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:segControlItems];
segControl.frame = CGRectMake(0, 0, 90, 30);
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
segControl.momentary = YES;
segControl.tintColor = [UIColor greenColor];
[segControl addTarget:self action:#selector(segAction:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segBarItem = [[UIBarButtonItem alloc] initWithCustomView:segControl];
self.switchView.navigationItem.rightBarButtonItem = segBarItem;
[segControl release];
[segBarItem release];
Has apple made some property like "makeUISegmentedControlsDontSetAnyTintColor" ore soo?
Please help...
A single button works: http://img257.imageshack.us/i/schermafbeelding2010102.png/
But with the segmented it's messed up: http://img714.imageshack.us/i/schermafbeelding2010102.png/
I've done it without problems using InterfaceBuilder. I had three segments, some with labels, some with labels + pictures, all working fine.
It must be your conversion into a BarButtonItem that doesn't work. You could try adding the segmented control as a subview of the nav bar instead of coaxing it into a button.
This is a really old question but I ran into this problem today (Xcode 7b5/iOS 9). In my case the segmentedControl was a UIBarButtonItem in the navBar. I was able to set the tintColor by waiting for the next run loop, like so:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] init]
(...additional setup...)
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.navigationItem.rightBarButtonItem = segmentBarItem;
dispatch_async(dispatch_get_main_queue(), ^{
segmentedControl.tintColor=[UIColor purpleColor];
});

How can I add two UIBarButtonItems to UINavigationItem?

I want two rightBarButtonItem's on my UINavigationBar. How can I accomplish this?
You can use a UISegmentedControl with two buttons and configure it with the momentary property set to YES.
This is what is used in the Mail application to go to next/previous message.
Update
In order to assign the UISegmentedControl]1 as a right button, you have to wrap it inside a UIBarButtonItem (sample code taken from the NavBar sample application):
- (void)viewDidLoad
{
// "Segmented" control to the right
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
}

Adding buttons to navigation bar

Is it possible to add buttons to the navigation bar using the IPhone SDK?
I already have 2 buttons in the navigation bar as leftBarButton and rightBarButton. I need 2 more buttons. How to implement that?
Its not obligatory that i need them to be included in the navigation bar itself. But since the application contains only a table, i don't think it can be given elsewhere.
You can use the UISegmentedControl. Check the UICatalog code sample to check its usage in the navigation bar.
Here is some sample code:
- (void)viewDidLoad {
[super viewDidLoad];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
}
- (void)segmentAction:(id)sender{
if([sender selectedSegmentIndex] == 0){
//do something with segment 1
NSLog(#"Segment 1 preesed");
}else{
//do something with segment 2
NSLog(#"Segment 2 preesed");
}
}

Disabling UINavigationItem custom right view?

I create a custom right view as follows:
// Build the Segmented Control
NSArray *segmentTextContent = [NSArray arrayWithObjects:[UIImage imageNamed:#"arrow-dice.png"], [UIImage imageNamed:#"arrow-up.png"], [UIImage imageNamed:#"arrow-down.png"], nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
// Customize the Segmented Control
segmentedControl.momentary = YES;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:#selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
Then I add it to my navigation bar as follows:
// Add the control to the navigation bar right item
UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentItem;
self.navigationItem.rightBarButtonItem.title = #"";
[segmentItem release];
I can hide it as follows:
self.navigationItem.rightBarButtonItem.customView.hidden = NO;
QUESTION
...but how can I disable all (or better, a specific element) of the segmented control?
The following does not work.
self.navigationItem.rightBarButtonItem.enabled = NO;
Any ideas appreciated...
Thanks,
matt
UISegmentedControl.h
- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
- (void)removeAllSegments;
- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment; //default is YES
e.g. [segmentedControl setEnabled:NO forSegmentAtIndex:1];
Hope That helps
[navItem.rightBarButtonItem setEnabled:NO];
as simple as that :)
Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBarItem_Class/Reference/Reference.html
See the enabled property.