This article reprinted to http://www.cnblogs.com/tx8899/archive/2012/06/05/2537020.html
To add a staging control
A control is a small, self-contained UI component that can be used in a variety of UI kit classes. They can be attached to many different types of objects, allowing developers to add additional functionality to the window. One control is often seen in the navigation bar of an apple-preinstalled application, which is the segmented control.
You'll notice that in many pre-built apps, Apple adds buttons to further classify the displayed information. For example, the ITunes WiFi store app's navigation bar displays buttons such as "latest release," "Hot Hits," "genre," and so on. These buttons further divide the user's song selection method. Segmented controls are suitable for situations where there is a lot of similar data, preferably two to three buttons to classify the data.
The following example is equipped with a control that displays "All" and "missed" calls:
- Uisegmentedcontrol *Segmentedcontrol = [[Uisegmentedcontrol alloc]
- Initwithitems:nil];
- Segmentedcontrol.segmentedcontrolstyle =
Uisegmentedcontrolstylebar;
- [Segmentedcontrol Insertsegmentwithtitle:
@ "All" atindex:0 animated:no];
- [Segmentedcontrol Insertsegmentwithtitle:
@ "Missed" atindex:1 Animated:no];
Once you've created a segmented control, you can give it a view controller's Titleview navigation property to display it. This will allow the standard title text to be replaced by your custom view:
- Self.navigationItem.titleView = Segmentedcontrol;
You should also want this class to be notified immediately when a user chooses a new segment, so that it can be changed to show new information. To do this, you can use the Addtarget method of the Uicontrol class to specify a method to invoke when the value of the control changes:
- [Segmentedcontrol addtarget:self
- Action: @selector (controlpressed:)
- Forcontrolevents:uicontroleventvaluechanged
- ];
In the following example, a selector named controllerpressed is specified as the method that should be notified in the target self. Please write this routine in your target class to handle the change in value:
- -(void) controllerpressed: (ID) Sender {
- int SelectedIndex = [Segmentedcontrol selectedsegmentindex];
- /* Add code to handle changes in values */
- }
Each button in a segmented control is referred to as a segment. By invoking the Selectedsegment method of the control itself, you can access the selected segment:
- -(void) controllerpressed: (ID) Sender {
- int selectedsegment = segmentedcontrol.selectedsegmentindex;
- NSLog (@ "Segment%d selected\n", selectedsegment);
- }
ios-adding a segmented control Segmentcontrol