ios-adding a segmented control Segmentcontrol

Source: Internet
Author: User

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:

    1. Uisegmentedcontrol *Segmentedcontrol = [[Uisegmentedcontrol alloc]
    2. Initwithitems:nil];
    3. Segmentedcontrol.segmentedcontrolstyle =
      Uisegmentedcontrolstylebar;
    4. [Segmentedcontrol Insertsegmentwithtitle:
      @ "All" atindex:0 animated:no];
    5. [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:

    1. 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:

    1. [Segmentedcontrol addtarget:self
    2. Action: @selector (controlpressed:)
    3. Forcontrolevents:uicontroleventvaluechanged
    4. ];

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:

    1. -(void) controllerpressed: (ID) Sender {
    2. int SelectedIndex = [Segmentedcontrol selectedsegmentindex];
    3. /* Add code to handle changes in values */
    4. }

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:

      1. -(void) controllerpressed: (ID) Sender {
      2. int selectedsegment = segmentedcontrol.selectedsegmentindex;
      3. NSLog (@ "Segment%d selected\n", selectedsegment);
      4. }

ios-adding a segmented control Segmentcontrol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.