UISegmentedControl for IOS development (5)

Source: Internet
Author: User

1 Preface
UISegmentedController is similar to a single-choice button and provides a selection operation. Today we will learn about its simple usage.


2 UISegmentedController Introduction
Code on

. H file


[Plain]
# Import <UIKit/UIKit. h>
 
@ Interface ZYViewController: UIViewController
 
@ Property (nonatomic, strong) UISegmentedControl * mySegmentedControl;
 
@ End

# Import <UIKit/UIKit. h>

@ Interface ZYViewController: UIViewController

@ Property (nonatomic, strong) UISegmentedControl * mySegmentedControl;

@ End
. M file:


[Plain]
@ Synthesize mySegmentedControl;
 
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
// Initialize the content Array
NSArray * segments = [[NSArray alloc] initWithObjects: @ "iPhone", [UIImage imageNamed: @ "ipad.png"], @ "iPod", @ "iMac", nil];
// NSArray * segments = [[NSArray alloc] initWithObjects: @ "iPhone", @ "iPad", @ "iPod", @ "iMac", nil];
// Construct the shape of mySegmentedControl
Self. mySegmentedControl = [[UISegmentedControl alloc] initWithItems: segments];
CGRect segmentedFrame = self. mySegmentedControl. frame;
SegmentedFrame. size. height = 64.0f;
SegmentedFrame. size. width = 300366f;
Self. mySegmentedControl. frame = segmentedFrame;
Self. mySegmentedControl. center = self. view. center;
Self. mySegmentedControl. segmentedControlStyle = UISegmentedControlStyleBar; // modify the style
Self. mySegmentedControl. momentary = YES; // after an item is selected, the status changes back to the original state.
[Self. mySegmentedControl addTarget: self action: @ selector (segmentChanged :) forControlEvents: UIControlEventValueChanged]; // Add an event
[Self. view addSubview: self. mySegmentedControl];
}
 
-(Void) segmentChanged :( UISegmentedControl *) paramSender {
If ([paramSender isEqual: self. mySegmentedControl]) {
// Obtain the index location
NSInteger selectedSegmentIndex = [paramSender selectedSegmentIndex];
// Obtain the text value of the corresponding index location
NSString * selectedSegmentText = [paramSender titleForSegmentAtIndex: selectedSegmentIndex];
NSLog (@ "Segment % ld with % @ text is selected", (long) selectedSegmentIndex, selectedSegmentText );
}
}

@ Synthesize mySegmentedControl;

-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
// Initialize the content Array
NSArray * segments = [[NSArray alloc] initWithObjects: @ "iPhone", [UIImage imageNamed: @ "ipad.png"], @ "iPod", @ "iMac", nil];
// NSArray * segments = [[NSArray alloc] initWithObjects: @ "iPhone", @ "iPad", @ "iPod", @ "iMac", nil];
// Construct the shape of mySegmentedControl
Self. mySegmentedControl = [[UISegmentedControl alloc] initWithItems: segments];
CGRect segmentedFrame = self. mySegmentedControl. frame;
SegmentedFrame. size. height = 64.0f;
SegmentedFrame. size. width = 300366f;
Self. mySegmentedControl. frame = segmentedFrame;
Self. mySegmentedControl. center = self. view. center;
Self. mySegmentedControl. segmentedControlStyle = UISegmentedControlStyleBar; // modify the style
Self. mySegmentedControl. momentary = YES; // after an item is selected, the status changes back to the original state.
[Self. mySegmentedControl addTarget: self action: @ selector (segmentChanged :) forControlEvents: UIControlEventValueChanged]; // Add an event
[Self. view addSubview: self. mySegmentedControl];
}

-(Void) segmentChanged :( UISegmentedControl *) paramSender {
If ([paramSender isEqual: self. mySegmentedControl]) {
// Obtain the index location
NSInteger selectedSegmentIndex = [paramSender selectedSegmentIndex];
// Obtain the text value of the corresponding index location
NSString * selectedSegmentText = [paramSender titleForSegmentAtIndex: selectedSegmentIndex];
NSLog (@ "Segment % ld with % @ text is selected", (long) selectedSegmentIndex, selectedSegmentText );
}
} Result after running:

 
 


After clicking each button, the control is displayed as follows:


10:30:00. 543 UISegmentedControllerTest [985: c07] Segment 0 with iPhone text is selected

10:30:01. 045 UISegmentedControllerTest [985: c07] Segment 1 with (null) text is selected

10:30:01. 501 UISegmentedControllerTest [985: c07] Segment 2 with iPod text is selected

10:30:01. 862 UISegmentedControllerTest [985: c07] Segment 3 with iMac text is selected

 

Related Article

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.