[Code Note] single choice box,
I ,.
2. Engineering Drawing.
3. Code.
RootViewController. h
# Import <UIKit/UIKit. h> @ interface RootViewController: UIViewController // you can easily jump to the corresponding page @ property (nonatomic, retain) NSString * type; @ end
RootViewController. m
# Import "RootViewController. h "@ interface RootViewController () @ end @ implementation RootViewController {int butTag;} @ synthesize type;-(id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) else {self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (self) {// Custom initialization} return self;}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. self. title = @ "Choice box"; self. view. backgroundColor = [UIColor orangeColor]; NSArray * array = [NSArray arrayWithObjects: @ "student", @ "parent", @ "instructor", nil]; for (int I = 0; I <3; I ++) {// Label UILabel * lab = [UILabel alloc] initWithFrame: CGRectMake (60 + 80 * I, 100, 50, 15)]; [lab setFont: [UIFont systemFontOfSize: 15]; [lab setTextColor: [UIColor grayColor]; [lab setBackgroundColor: [UIColor clearColor]; [lab setText: [array objectAtIndex: I]; [self. view addSubview: lab]; butTag = 100; // click UIButton * but = [UIButton buttonWithType: UIButtonTypeCustom]; [but setFrame: CGRectMake (40 + 80 * I, 100, 15, 15)]; [but setImage: [UIImage imageNamed: @ "1"] forState: UIControlStateNormal]; [but setImage: [UIImage imageNamed: @ "2"] forState: UIControlStateSelected]; [but addTarget: self action: @ selector (choose :) forControlEvents: UIControlEventTouchUpInside]; [but setTag: I + 100]; [self. view addSubview: but]; if (butTag = I + 100) {[but setSelected: YES]; [but setUserInteractionEnabled: NO] ;}}} // select the role-(void) choose :( UIButton *) sender {UIButton * button = (UIButton *) [self. view viewWithTag: butTag]; [button setSelected: NO]; [button setUserInteractionEnabled: YES]; [sender setUserInteractionEnabled: NO]; [sender setSelected: YES]; int intvalue = (int) sender. tag-100; NSString * typeStr = [NSString stringWithFormat: @ "% I", intvalue]; self. type = typeStr; NSLog (@ "self. type = % @ ", self. type); butTag = (int) sender. tag;}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .}
4. Output:
13:59:33. 313 select box [7842: 203451] self. type = 12015-10-13 13:59:33. 921 select box [7842: 203451] self. type = 22015-10-13 13:59:34. 569 select box [7842: 203451] self. type = 12015-10-13 13:59:35. 345 select box [7842: 203451] self. type = 02015-10-13 13:59:36. 897 select box [7842: 203451] self. type = 1