一,效果图。

二,工程图。

三,代码。

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. 【Android】Fragment的简单笔记

    被虐了,做某公司笔试时,发现自己连个Fragment的生命周期都写不详细.平时敲代码,有开发工具的便利,有网上各大神的文章,就算忘了也很容易的可以查到,但当要自己不借助外界,却发现自己似乎对该知识点并 ...

  2. winform中ComboBox利用AutoComplete属性实现模糊查询(有缺陷)

    上一篇文章是用ComboBox里面的原生事件实现模糊查询,操作比较灵活一些,但是收到评论说,利用AutoComplete属性就可以实现模糊查询,但是据本人所了解,AutoComplete虽然能够方便的 ...

  3. 自己封装的JS分页功能[用于搭配后台使用]

    * 2016.7.03 修复bug,优化代码逻辑 * 2016.5.25 修复如果找不到目标对象的错误抛出. * 2016.5.11 修复当实际页数(pageNumber)小于生成的页码间隔数时的bu ...

  4. ChatRichTextBox : RichTextBox

    using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using ...

  5. jquery.form.js实现异步上传

    前台页面 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...

  6. SQLite中文排序

    定义一个类: using System.Data.SQLite; namespace DAL { /// <summary> /// SQLite中文排序 /// </summary ...

  7. C#执行存储过程的简化

    下面的方法是我在实际开发中摸索出来的,可以在很大程度上简化调用存储过程的代码. 首先来看一下C#调用存储过程的一般过程:1.打开数据库连接SqlConnection:2.生成一个SqlCommand: ...

  8. go语言 hello 小结

    在编译go语言的时候: 写了一段这样的代码 package main import "fmt" func main() {     fmt.Println("Hello, ...

  9. Swift 值类型和引用类型

    Swift中的类型分为两类:一,值类型(value types),每个值类型的实例都拥有各自唯一的数据,通常它们是结构体,枚举或元组:二,引用类型(reference types),引用类型的实例共享 ...

  10. ASP.NET MVC SSO单点登录设计与实现(转载)

    实验环境配置 HOST文件配置如下: 127.0.0.1 app.com127.0.0.1 sso.com IIS配置如下: 应用程序池采用.Net Framework 4.0 注意IIS绑定的域名, ...