一,效果图。

二,工程图。

三,代码。

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

//方便跳转到相应的页面
@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 *)nibBundleOrNil
{
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=@"选择框"; self.view.backgroundColor=[UIColor orangeColor]; NSArray *array=[NSArray arrayWithObjects:@"学生",@"家长",@"老师", 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 ; //能点击的小圆点按钮
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];
}
} } //选择角色
- (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)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

四,输出:

2015-10-13 13:59:33.313 选择框[7842:203451] self.type = 1
2015-10-13 13:59:33.921 选择框[7842:203451] self.type = 2
2015-10-13 13:59:34.569 选择框[7842:203451] self.type = 1
2015-10-13 13:59:35.345 选择框[7842:203451] self.type = 0
2015-10-13 13:59:36.897 选择框[7842:203451] self.type = 1

【代码笔记】iOS-单项选择框的更多相关文章

  1. JavaScript学习笔记之下拉选择框的操作

    对于下拉框的操作十分繁多,这几天项目须要就总结一下 一.动态构建option 有时候我们须要动态构建下拉选择框里面的值,这里我们就要用到 var varItem = new Option(" ...

  2. 【代码笔记】iOS-先选择城市,然后,跳转Tabbar

    一,效果图. 二,工程图. 三,代码. ChooseCityViewController.h #import <UIKit/UIKit.h> @interface ChooseCityVi ...

  3. 【代码笔记】Web-ionic单选框

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  4. 左右选择框 js插件

    随着项目的进展,测试工程师在更多的浏览器中兼容性测试中,发现有些浏览器不支持option的触发事件,这就造成了先前一篇博文bootstrap 左右框多项选择示例 中左右选择框的失效,于是我就由原先的s ...

  5. 【代码笔记】iOS-时间选择框

    一, 效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewControlle ...

  6. 【代码笔记】iOS-点击出现选择框

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  7. 【代码笔记】iOS-自定义选择框

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import "CYCustomMultiSe ...

  8. Java开发笔记(一百二十二)AWT选择框

    前面介绍了两种文本输入框的用法,不过实际应用很少需要用户亲自文字,而是在界面上列出几个选项,让用户勾勾点点完成选择,这样既方便也不容易弄错.依据选择的唯一性,可将选项控件分为两类:一类是在方框中打勾的 ...

  9. Java开发笔记(一百三十)Swing的选择框

    不管是AWT还是Swing,都把选择框分成两类:复选框和单选按钮,这两类控件无论是外观上还是功能上均有显著差异.例如,在外观方面,复选框是在方框内打勾,而单选按钮是在圆圈内画圆点:在功能方面,复选框允 ...

随机推荐

  1. 微信开发系列----02:实现POST请求响应

    继续昨天的,现在我们的微信测试成功了,可以开发实现微信的各种功能,今天主要实现微信的简单交互,比如发送语音,图片,文本等请求,网站服务器发送对应的响应. 项目GitHub地址:  https://gi ...

  2. Log4net 日志使用介绍

    概述 Log4net 有三个主要组件:loggers,appenders 和 layouts.这三个组件一起工作使得开发者能够根据信息类型和等级(Level)记录信息,以及在运行时控制信息的格式化和信 ...

  3. 【转载】MVC使用HandleErrorAttribute自定义异常

    本文导读:在ASP.NET MVC中,可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特 ...

  4. Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive.

    调试网站时,异常出现:Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive. S ...

  5. C#无限极分类树-创建-排序-读取 用Asp.Net Core+EF实现

    今天做一个管理后台菜单,想着要用无限极分类,记得园子里还是什么地方见过这种写法,可今天找了半天也没找到,没办法静下心来自己写了: 首先创建节点类(我给它取名:AdminUserTree): /// & ...

  6. SQL(触发器)

    触发器语法格式(实例): ALTER TRIGGER tri_edituserON dbo.sys_UserFOR INSERT,UPDATE,DELETEAS BEGIN ---判断是新增?修改?删 ...

  7. 清除Windows的DNS缓存

    最近ESET杀毒软件老是提示受到DNS缓存攻击,然后就不能打开网页,或者打开得很慢.这是由于缓存的DNS被更改,访问的是错误的IP地址造成的. 解决的办法就是清除DNS缓存,打开DOS命令窗口,先后输 ...

  8. 2015暑假多校联合---CRB and His Birthday(01背包)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthda ...

  9. ThinkCMF-上传多个图片源码解读

    关键函数: /** * 多图上传 * @param dialog_title 上传对话框标题 * @param container_selector 图片容器 * @param item_tpl_wr ...

  10. MyBatis入门(一)

    一.MyBaris简介 1)MyBaris发展过程 MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apache software foundation 迁移 ...