iPhone控件之UISwitch

 1 //
2 // UITestViewController.m
3 // UITest
4 //
5
6 #import "UITestViewController.h"
7
8 @implementation UITestViewController
9
10 -(void)switchAction:(id)sender
11 {
12 NSLog(@"switch changed");
13 }
14
15 - (void)viewDidLoad {
16
17 [super viewDidLoad];
18
19 CGRect switchRect = CGRectMake(120,50,0,0);
20 UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:switchRect];
21 [mySwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
22
23 //customize the appearance
24 UIView *mainView = [[[[mySwitch subviews] objectAtIndex:0] subviews] objectAtIndex:2];
25 UILabel *onLabel = [[mainView subviews] objectAtIndex:0];
26 UILabel *offLabel = [[mainView subviews] objectAtIndex:1];
27
28 //change the text
29 onLabel.text = @"YES";
30 offLabel.text = @"NO";
31
32 //change the text color
33 onLabel.textColor = [UIColor yellowColor];
34 offLabel.textColor = [UIColor greenColor];
35
36 [self.view addSubview:mySwitch];
37
38 [mySwitch release];
39 }
40
41 - (void)didReceiveMemoryWarning {
42 // Releases the view if it doesn't have a superview.
43 [super didReceiveMemoryWarning];
44
45 // Release any cached data, images, etc that aren't in use.
46 }
47
48 - (void)viewDidUnload {
49 // Release any retained subviews of the main view.
50 // e.g. self.myOutlet = nil;
51 }
52
53
54 - (void)dealloc {
55 [super dealloc];
56 }
57
58 @end
posted @ 2012-03-13 13:14  FoxBabe  阅读(277)  评论(0编辑  收藏  举报