1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
segmentControl.backgroundColor = [UIColor yellowColor];
[segmentControl insertSegmentWithTitle:@"第一段" atIndex:0 animated:YES];
[segmentControl insertSegmentWithTitle:@"第二段" atIndex:1 animated:YES];
[segmentControl insertSegmentWithTitle:@"第三段" atIndex:2 animated:YES]; UIImage *image = [UIImage imageNamed:@"qq"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[segmentControl setImage:image forSegmentAtIndex:2];
segmentControl.tintColor = [UIColor redColor];
segmentControl.selectedSegmentIndex = 1;
NSString *title = [segmentControl titleForSegmentAtIndex:1]; NSLog(@"title = %@",title);
[segmentControl addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:segmentControl]; } -(void)valueChange:(UISegmentedControl*)seg{
NSInteger index = seg.selectedSegmentIndex; switch (index) { case 0:{ NSLog(@"0"); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"还钱" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"不还", nil]; [alert show]; } break; case 1: NSLog(@"1"); break; case 2: NSLog(@"2"); break; default: break; } }
@end
|