[Code Note] single choice box,

Source: Internet
Author: User

[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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.