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