UISegmentedController 使用以及代码!

UISegmentedController类似于单选按钮,可以提供选择操作,今天我们就来学习一下其简单用法.

.h文件

[plain]

#import

@interface ZYViewController : UIViewController

@property(nonatomic,strong) UISegmentedControl *mySegmentedControl;

@end

#import

@interface ZYViewController : UIViewController

@property(nonatomic,strong) UISegmentedControl *mySegmentedControl;

@

.m文件:

[plain]

@synthesize mySegmentedControl;

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.view.backgroundColor = [UIColor whiteColor];

//初始化内容数组

NSArray *segments = [[NSArray alloc] initWithObjects:@"iPhone",[UIImage imageNamed:@"ipad.png"],@"iPod",@"iMac",nil];

//    NSArray *segments = [[NSArray alloc] initWithObjects:@"iPhone",@"iPad",@"iPod",@"iMac",nil];

//构造mySegmentedControl的外形

self.mySegmentedControl = [[UISegmentedControl alloc] initWithItems:segments];

CGRect segmentedFrame = self.mySegmentedControl.frame;

segmentedFrame.size.height = 64.0f;

segmentedFrame.size.width = 300.0f;

self.mySegmentedControl.frame = segmentedFrame;

self.mySegmentedControl.center = self.view.center;

self.mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;//修改样式

self.mySegmentedControl.momentary = YES;//选择了某项后,马上变回原始状态

[self.mySegmentedControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];//添加事件

[self.view addSubview:self.mySegmentedControl];

}

-(void)segmentChanged:(UISegmentedControl *)paramSender{

if ([paramSender isEqual:self.mySegmentedControl]) {

//获得索引位置

NSInteger selectedSegmentIndex = [paramSender selectedSegmentIndex];

//获得对应索引位置的text值

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

//初始化内容数组

NSArray *segments = [[NSArray alloc] initWithObjects:@"iPhone",[UIImage imageNamed:@"ipad.png"],@"iPod",@"iMac",nil];

//    NSArray *segments = [[NSArray alloc] initWithObjects:@"iPhone",@"iPad",@"iPod",@"iMac",nil];

//构造mySegmentedControl的外形

self.mySegmentedControl = [[UISegmentedControl alloc] initWithItems:segments];

CGRect segmentedFrame = self.mySegmentedControl.frame;

segmentedFrame.size.height = 64.0f;

segmentedFrame.size.width = 300.0f;

self.mySegmentedControl.frame = segmentedFrame;

self.mySegmentedControl.center = self.view.center;

self.mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;//修改样式

self.mySegmentedControl.momentary = YES;//选择了某项后,马上变回原始状态

[self.mySegmentedControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];//添加事件

[self.view addSubview:self.mySegmentedControl];

}

-(void)segmentChanged:(UISegmentedControl *)paramSender{

if ([paramSender isEqual:self.mySegmentedControl]) {

//获得索引位置

NSInteger selectedSegmentIndex = [paramSender selectedSegmentIndex];

//获得对应索引位置的text值

NSString *selectedSegmentText = [paramSender titleForSegmentAtIndex:selectedSegmentIndex];

NSLog(@"Segment %ld with %@ text is selected",(long)selectedSegmentIndex,selectedSegmentText);

}

}

end;

'以上就是UISegmentedController的代码以及使用!

你可能感兴趣的:(UISegmentedController 使用以及代码!)