UISegmentedCotrol Hidden - iphone

I created a UISegmentedControl through the following snippet.
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(105, 270, 140, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(selectWeek:) forControlEvents:UIControlEventValueChanged];
[self.navigationController.view addSubview:segmentedControl];
When I press the button to turn UISegmentedCotrol, it doesn't work. What could be wrong?
I'm doing segmentedControl.hidden = YES;.

[self.navigationController.view addSubview:segmentedControl];
change it to
[self.view addSubview:segmentedControl];
and make sure segmentedControl.hidden=NO; when you add it to subview;

Related

How to add an image & a system bar button item to a UISegmentedControl?

I have an UISegmentedControl as the navigation bar's right bar button item. This is achieved by the following code...
UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Segment1",#"Segment2",nil]];
[segmentedControl addTarget:self action:#selector(segmentClicked) forControlEvents:UIControlEventValueChanged]; //Where segmentClicked is the method for segment click action
segmentedControl.frame = CGRectMake(0, 0, 90, 35);
UIBarButtonItem *rightBaritem = [ [UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = rightBaritem;
[rightBaritem release];
The above code fine and it will show a segmented Control with two segments "Segment1" & "Segment2".
But I want to show an image instead of Segment1 & a system bar button (Say UIBarButtonSystemItemAdd) instead of Segment2.
Image can be inserted in Segmented control by the code,
UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:[UIImage imageNamed:#"<image_name.image_type>"],???????,nil]];
But i dont know how to include UIBarButtonSystemItemAdd in the place of ???????.
Thanks in Advance..
This code may solve your problem.This solved my problem that I was facing at the same time.But don't think this is the accurate one.I got the desired which I need to display.This works fine with two buttons.
UISegmentedControl *doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
doneButton.momentary = YES;
doneButton.frame = CGRectMake(240, 7.0f, 70.0f, 30.0f);
doneButton.tintColor = [UIColor blackColor];
[doneButton addTarget:self action:#selector(Done:) forControlEvents:UIControlEventValueChanged];
[menustyle addSubview:doneButton];
[doneButton release];
doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
doneButton.momentary = YES;
doneButton.frame = CGRectMake(10, 7.0f, 70.0f, 30.0f);
doneButton.tintColor = [UIColor blackColor];
[doneButton addTarget:self action:#selector(Krishna:) forControlEvents:UIControlEventValueChanged];
[menustyle addSubview:doneButton];
[doneButton release];

Toolbar item not showing

Not sure what I'm doing wrong. I don't have a nib so I'm making everything in loadView. The toolbar shows up but the segmentedControl does not.
- (void)loadView
{
// Toolbar
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
[toolbar setTintColor:[UIColor lightGrayColor]];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
NSArray *toolbarItems = [NSArray arrayWithObjects:item, nil];
[toolbar setItems:toolbarItems animated:NO];
[self.view addSubview:toolbar];
}
Write below code in place of your code; this will help you to add segment control to your toolbar:
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
[toolbar setTintColor:[UIColor lightGrayColor]];
CGRect frame;
frame.origin.x = 10;
frame.origin.y = 10;
frame.size.width = 200;
frame.size.height = 30;
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Hello",#"Hi", nil]];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
segmentedControl.tintColor = [UIColor blackColor];
segmentedControl.frame = frame;
[toolbar addSubview:segmentedControl];
[self.view addSubview:toolbar];
You've got to add it as a subview of the view as you did with the toolbar. I.e:
[self.view addSubview:segmentedControl];
It should work.
Cheers

UISegmentControl configuration

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 75, 25)];
label.textAlignment = UITextAlignmentRight;
label.tag = kLabelTag;
label.font = [UIFont boldSystemFontOfSize:14];
label.text = #"Lawn Sign";
[cell.contentView addSubview:label];
[label release];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
NSArray * item = [ NSArray arrayWithObjects:#"No",#"Install",#"Replace",#"Move", nil];
[segmentedControl initWithItems:item];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(90, 10, 200, 30);
[segmentedControl setMomentary:YES];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.tag = 14;
[cell.contentView addSubview:segmentedControl];
[segmentedControl release];
Here's my UISegmentedControl configuration for a cell in the tableview but when I tap on an option in the segmentedControl, it doesn't switch.
You've set the control to be momentary. Change this:
[segmentedControl setMomentary:YES];
to this:
[segmentedControl setMomentary:NO];
and the control will show its selected state.

When I select any segment in segmentControll it doesnot show highlighted

NSArray *itemArray = [NSArray arrayWithObjects:#"one", #"Two", #"Three", nil];
segmentControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentControl.frame = CGRectMake(5, 5, 325, 35);
segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentControl.tintColor = [UIColor blackColor];
[self changeUISegmentFont:segmentControl];
//[self.view addSubview:segmentControl];
self.navigationItem.titleView = segmentControl;
[segmentControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
This is my code of segment control and my problem is ..
When I select any segment it does not show me that that segment is selected..
I mean it is not shown highlighted..
can Anyone tell me what the problem is??
Change the following line in your code.
segmentControl.tintColor = [UIColor blackColor];
to
segmentControl.tintColor = [UIColor grayColor];
Actually the segment is getting selected but It is not visible to you, because black color is the default color in selected mode.

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];
}