Objective-C Learning Notes - UISegmentControl

按钮栏

    可以结合使用索引和实例方法 titleForSegmentAtIndex 来获取每个分段的标题.要获取分段控件中当前选定的标题,可以使用以下代码段:

    [mySegment titleForSegmentAtIndex:mySegment.selectSegmentIndex];


动态实现:


#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong,nonatomic)UISegmentedControl *mySegmentControl;
@end




#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize mySegmentControl;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *segmentdArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4", nil];
    
    
    UISegmentedControl *segmentTemp = [[UISegmentedControl alloc]initWithItems:segmentdArray];
    
    mySegmentControl = segmentTemp;
    
    mySegmentControl.frame = CGRectMake(60.0, 9.0, 200.0, 50.0);
    
    [mySegmentControl setTitle:@"two" forSegmentAtIndex:1];
    
    [mySegmentControl setImage:[UIImage imageNamed:@"1.png"] forSegmentAtIndex:3];
    
    [mySegmentControl insertSegmentWithImage:[UIImage imageNamed:@"0.jpg"] atIndex:2 animated:NO];
    
    [mySegmentControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];
    
    [mySegmentControl removeSegmentAtIndex:0 animated:NO];
    
    [mySegmentControl setWidth:70.0 forSegmentAtIndex:2];
    
    [mySegmentControl setContentOffset:CGSizeMake(9.0, 9.0) forSegmentAtIndex:1];
    
    UIImageView *imageForSegmentAtIndex = [[UIImageView alloc]initWithImage:[mySegmentControl imageForSegmentAtIndex:1]];
    
    imageForSegmentAtIndex.frame = CGRectMake(60.0, 100.0, 30.0, 30.0);
    
    UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 30.0, 30.0)];
    
    titleForSegmentAtIndex.text = [mySegmentControl titleForSegmentAtIndex:0];
    
    UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 100.0, 30.0, 30.0)];
    
    numberOfSegments.text = [NSString stringWithFormat:@"%lu",(unsigned long)mySegmentControl.numberOfSegments];
    
    UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 100.0, 70.0, 30.0)];
    
    widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[mySegmentControl widthForSegmentAtIndex:2]];
    
    mySegmentControl.selectedSegmentIndex = 2;
    
    mySegmentControl.tintColor = [UIColor redColor];
    
    mySegmentControl.momentary = YES;
    
    [mySegmentControl setEnabled:NO forSegmentAtIndex:4];
    BOOL enableFlag = [mySegmentControl isEnabledForSegmentAtIndex:4];
    
    NSLog(@"%d", enableFlag);
    
    [self.view addSubview:widthForSegmentAtIndex];
    [self.view addSubview:numberOfSegments];
    [self.view addSubview:titleForSegmentAtIndex];
    [self.view addSubview:imageForSegmentAtIndex];
    [self.view addSubview:mySegmentControl];
    
    //[mySegmentControl removeAllSegments];
    
    
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end


标签:ios, object-c, uisegmentcontrol