10个控件
label
button
view
imageview
textfield
textview
interface build IBOutlet 界面显示
IBAction 和void一个类型
计步器
UIStepper * stepper
进度条
UIProgressView * progress
警告框
1、添加的警告框的标题
2、警告框的内容
3、设置代理 一般设置当前类的对象指针为UIAlertView的代理
4、设置按钮的标题 此处的标题一般的时候不缺省
5、设置多个按钮标题
UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:@"警告" message:@"你很帅" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:@"呵呵", nil];
//alerview 就是与用户交互的警告框
//buttonIndex 警告框上的按钮的下标值(从左向右,或者从上向下,下标值逐渐加1)
//cancelButton 的下标值永远为0
提示框
//destructiveButton 的下标值为0 , 按钮从上到下,下标值逐个加1
UIActionSheet * sheet = [[UIActionSheet alloc]initWithTitle:@"上传图片信息" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"拍照",@"从相册中获取",@"从本地获取", nil];
//将提示框添加到视图上面
分段控件对象
NSArray * arr = @[@"red",@"green",@"blue"];
UISegmentedControl * segmentcontrol = [[UISegmentedControl alloc]initWithItems:arr];
segmentcontrol.frame = CGRectMake(10, 100, 300, 40);
创建滑动块
UISlider * slider = [[UISlider alloc]initWithFrame:CGRectMake(10, 100, 300, 10)];
网页视图WEBview
UIWebView * webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 50, 320, 480-50)];
创建开关控件对象 及活动指示器
UISwitch * swtichview = [[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
//活动指示器不能与用户进行交互
//经常用于显示加载中
//<1>创建活动指示器对象
/*
UIActivityIndicatorViewStyleWhiteLarge,
UIActivityIndicatorViewStyleWhite,
UIActivityIndicatorViewStyleGray
*/
UIActivityIndicatorView * activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; //活动指示器
创建选择器控件UIPickerView
picker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 230, 320, 480-230)];
picker.backgroundColor = [UIColor greenColor];
//设置代理 必须设置代理,不然无法传值
picker.delegate = self;
picker.dataSource = self;
计步器
//<1>创建计步器对象
UIStepper * stepper = [[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
进度条
//进度条不能与用户进行交互
//必须让外界触发
/*
UIProgressViewStyleDefault,
UIProgressViewStyleBar
*/
UIProgressView * progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
//重写viewWillAppear方法,对视图上的数据或者布局进行修改
-(void)viewWillAppear:(BOOL)animated
{
UIStepper * stepper = (UIStepper *)[self.view viewWithTag:99];
UIProgressView * progressview = (UIProgressView *) [self.view viewWithTag:100];
stepper.value = 0;
progressview.progress = 0;
}
-(void)createStepper // 计步器
{
//<1>创建计步器对象
UIStepper * stepper = [[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//<2>设置stepper的背景颜色 默认背景颜色为白色和按钮一样的默认颜色
//stepper.backgroundColor = [UIColor redColor];
//<3>设置stepper的边框颜色
//默认为蓝色
stepper.tintColor = [UIColor purpleColor];
//<4>设置stepper的最小值
stepper.minimumValue = 0;
//<5>设置stepper的最大值
stepper.maximumValue = 100;
//<6>设置初始值
stepper.value = 0;
//<7>为stepper添加点击事件
[stepper addTarget:self action:@selector(stepperChange:) forControlEvents:UIControlEventValueChanged];
//<8>设置stepper的步长(点击+ - 的时候stepper的value的值的变化
stepper.stepValue = 10;
//<9>禁止长按的属性
//默认情况下是允许长按的
stepper.autorepeat = NO;
//stepper.continuous = NO; //过时了,5.0 不能用
stepper.tag = 99;
[self.view addSubview:stepper];
[stepper release];
}
-(void)stepperChange:(id)sender
{
//id表示任意类型的控件对象
//sender表示用户交互的控件, 这种用户交互机制所有控件都具有
UIStepper * stepper = (UIStepper *)sender;
UIProgressView * progressview = (UIProgressView *)[self.view viewWithTag:100];
progressview.progress = stepper.value/stepper.maximumValue;
if (progressview.progress == 1) {
NExTViewController * next = [[NExTViewController alloc]init];
[self presentViewController:next animated:YES completion:Nil];
[next release];
}
}
-(void)createProgress //进度器
{
//进度条不能与用户进行交互
//必须让外界触发
/*
UIProgressViewStyleDefault,
UIProgressViewStyleBar
*/
UIProgressView * progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
//<1>设置显示位置
progress.frame = CGRectMake(10, 200, 300, 10);
//<2>设置进度条左侧的颜色
progress.progressTintColor = [UIColor redColor];
//<3>设置进度条右侧的颜色
progress.trackTintColor = [UIColor blueColor];
//<4>设置进度条的初始值
progress.progress = 0;
//progress值从0到1
//如果设置的这progress的值大于0,那么进度条永远都处于充满的状态
progress.tag = 100;
[self.view addSubview:progress];
[progress release];
}
创建警告框
-(void)createBtn
{
UIButton * btn1 = [XLMyButton creatButtonWithFrame:CGRectMake(100, 100, 100, 100) target:self sel:@selector(perssBtn:) tag:1 image:Nil title:@"警告框"];
[self.view addSubview:btn1];
UIButton *btn2 = [XLMyButton creatButtonWithFrame:CGRectMake(100, 200, 100, 100) target:self sel:@selector(perssBtn:) tag:2 image:nil title:@"高仿警告框"];
[self.view addSubview:btn2];
}
-(void)perssBtn:(id)sender
{
UIButton * btn = (UIButton *)sender;
if (btn.tag == 1) {
//创建警告框
/*
1、添加的警告框的标题
2、警告框的内容
3、设置代理 一般设置当前类的对象指针为UIAlertView的代理
4、设置按钮的标题 此处的标题一般的时候不缺省
5、设置多个按钮标题
*/
UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:@"警告" message:@"你很帅" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:@"呵呵", nil];
//将alerview添加到视图上
[alertview show]; //警告框独有的方法
alertview.tag = 100;
[alertview release];
//alertview不需要设置显示位置及大小,编译器会自动设置警告显示在视图的中心位置
//alertview显示出来以后,后面的原有视图不能与用户交互,直到警告框消失才可与用户交互
}
else
{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"测试性格" message:@"选择颜色" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"红",@"黄",@"蓝",@"绿",nil];
[alert show];
[alert release];
}
// 点击警告框上的按钮,会自动触发协议中的方法
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//alerview 就是与用户交互的警告框
//buttonIndex 警告框上的按钮的下标值(从左向右,或者从上向下,下标值逐渐加1)
//cancelButton 的下标值永远为0
if (alertView.tag == 100) {
if (buttonIndex == 0) {
NSLog(@"点击的是:我知道了");
}
else
{
NSLog(@"点击的是:呵呵 按钮");
}
}
else
{
switch (buttonIndex) {
case 1:
self.view.backgroundColor = [UIColor redColor];
break;
case 2:
self.view.backgroundColor = [UIColor yellowColor];
break;
case 3:
self.view.backgroundColor = [UIColor blueColor];
break;
case 4:
self.view.backgroundColor= [UIColor greenColor];
break;
default:
break;
}
}
}
提示框
-(void)perssBtn:(id)sender
{
//提示框的显示位置及大小都是系统自动设置的,无需手动设置
//提示框显示位置在视图底的位置,向上弹出
//destructiveButton 和alertview中的cancelbutton相同,一般的时候这个按钮不能缺省
//destructiveButton 的下标值为0 , 按钮从上到下,下标值逐个加1
UIActionSheet * sheet = [[UIActionSheet alloc]initWithTitle:@"上传图片信息" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"拍照",@"从相册中获取",@"从本地获取", nil];
//将提示框添加到视图上面
[sheet showInView:self.view];//提示框独有的方法
[sheet release];
}
//点击按钮触发协议中的方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%d",buttonIndex);
}
分段控件对象
//分段控件
-(void)createSegment
{
//<1>创建分段控件对象
NSArray * arr = @[@"red",@"green",@"blue"];
UISegmentedControl * segmentcontrol = [[UISegmentedControl alloc]initWithItems:arr ];
segmentcontrol.frame = CGRectMake(10, 100, 300, 40);
//<2>设置分段控件的边框颜色
segmentcontrol.tintColor = [UIColor purpleColor];
//<3>设置默认选中的分段位置
segmentcontrol.selectedSegmentIndex = 0;
//<4>插入新的分段
[segmentcontrol insertSegmentWithTitle:@"new" atIndex:0 animated:YES];
//<5>点击事件
[segmentcontrol addTarget:self action:@selector(perssBtn:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentcontrol];
[segmentcontrol release];
}
-(void)perssBtn:(id)sender
{
UISegmentedControl * segment = (UISegmentedControl *)sender;
if (segment.selectedSegmentIndex == 1) {
self.view.backgroundColor = [UIColor redColor];
}
else if (segment.selectedSegmentIndex == 2)
{
self.view.backgroundColor = [UIColor blueColor];
}
else
{
self.view.backgroundColor =[UIColor whiteColor];
}
}
创建滑动块
-(void)createSlider
{
//<1>创建滑动块
UISlider * slider = [[UISlider alloc]initWithFrame:CGRectMake(10, 100, 300, 10)];
//<2>设置slider的最大值
slider.maximumValue = 1;
//<3>设置最小值
slider.minimumValue = 0;
//<4>设置slider的初始值
slider.value = 0;
//<5>设置slider的左侧的颜色
slider.minimumTrackTintColor = [UIColor redColor];
//<6>设置slider右侧的颜色
slider.maximumTrackTintColor = [UIColor yellowColor];
//<7>为slider添加事件 ,能与用户交互
[slider addTarget:self action:@selector(sliderChsanged:) forControlEvents:UIControlEventValueChanged];
slider.tag = 1;
[self.view addSubview:slider];
[slider release];
UISlider * slider1 = [[UISlider alloc]initWithFrame:CGRectMake(10, 200,300, 30)];
slider1.minimumValue = 0;
slider1.maximumValue = 1;
slider1.minimumTrackTintColor = [UIColor cyanColor];
slider1.maximumTrackTintColor = [UIColor magentaColor];
slider1.value = 0;
slider1.tag = 2;
//设置slider左侧图片
slider1.minimumValueImage = [UIImage imageNamed:@"tab_c1.png"];
//设置slider右侧图片
slider1.maximumValueImage = [UIImage imageNamed:@"tab_c3.png"];
//设置slider按钮图片
[slider1 setThumbImage:[UIImage imageNamed:@"tab_c0.png"] forState:UIControlStateNormal];
[slider1 addTarget:self action:@selector(sliderChsanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview: slider1];
[slider1 release];
}
-(void)sliderChsanged:(id)sender
{
UISlider * slider = (UISlider * )sender;
if (slider.tag == 1) {
NSLog(@"%f",slider.value);
}
else
{
if (slider.value < 1) {
static int count = 10;
count ++;
self.view.backgroundColor = [UIColor colorWithRed:(count * 25%255)/255.0 green:(count * 26%255)/255.0 blue:(count * 29%255)/255.0 alpha:1];
}
}
}
网页视图WEBview
-(void)createWebView
{
//<1>创建webview的对象,显示网址下的所有数据
UIWebView * webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 50, 320, 480-50)];
//<2>设置字符串网址
NSString * path = @"http://10.0.8.8";
//<3>将字符串转化成NSURL
NSURL * url = [NSURL URLWithString:path];
//<4>将NSURL封装成请求对象
NSURLRequest * request = [NSURLRequest requestWithURL:url];
//<5>将请求下来的数据显示在webview上
[webview loadRequest:request];
webview.tag = 1;
[self.view addSubview: webview];
[webview release];
}
-(void)createSlider
{
UISlider * slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 30, 320, 10)];
slider.minimumValue = 10;
slider.maximumValue = 1000;
slider.value = 10;
[slider addTarget:self action:@selector(shliderchanged:) forControlEvents:UIControlEventValueChanged];
slider.tag =100;
[self.view addSubview: slider];
[slider release];
}
-(void)shliderchanged:(id)sender
{
UIWebView * web = (UIWebView *)[self.view viewWithTag:1];
UISlider * slider =(UISlider *)[self.view viewWithTag:100];
[web stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%f%%'",slider.value]];
[web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'blue'"];
}
<1>创建开关控件对象 及活动指示器
-(void)createSwitch
{
//<1>创建开关控件对象
UISwitch * swtichview = [[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
//<2>设置开关按钮初始状态
swtichview.on = NO;
//<3>设置开关按钮开启时候的背景颜色
swtichview.onTintColor = [UIColor yellowColor];
//<4>设置开关按钮关闭时的颜色
swtichview.tintColor = [UIColor cyanColor];
//<5>设置开关按钮上按钮的颜色
swtichview.thumbTintColor = [UIColor redColor];
//<6>添加点击事件
[swtichview addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: swtichview];
[swtichview release];
}
-(void)switchChanged:(id)sender
{
UISwitch * twmpSwitch = (UISwitch *)sender;
UIActivityIndicatorView * activity = (UIActivityIndicatorView *)[self.view viewWithTag:200];
//判断开关按钮的状态
//tempSwitch.isOn表示开启状态
if (twmpSwitch.isOn) {
activity.hidden = NO;
[activity startAnimating]; //开始动画
}
else
{
[activity stopAnimating]; //停止动画
//hidesWhenStopped 为yes ,所以动画停止的时候自动隐藏,此处无需设置隐藏状态
}
}
-(void)createActivity
{
//活动指示器不能与用户进行交互
//经常用于显示加载中
//<1>创建活动指示器对象
/*
UIActivityIndicatorViewStyleWhiteLarge,
UIActivityIndicatorViewStyleWhite,
UIActivityIndicatorViewStyleGray,
*/
UIActivityIndicatorView * activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
//<2>设置活动指示器初始状态为隐藏
activity.hidden = YES;
//<3>设置活动指示器停止活动的时候,隐藏
activity.hidesWhenStopped = YES;
//<4>设置活动指示器的显示位置
activity.frame = CGRectMake(100, 200, 50, 50);
activity.tag = 200;
[self.view addSubview:activity];
[activity release];
}
选择器控件UIPickerView //数组随意设置
遵守协议.h
<UIPickerViewDelegate,UIPickerViewDataSource>
- (void)viewDidLoad
{
[super viewDidLoad];
arr1 = [[NSArray alloc]initWithObjects:@"水",@"空气",@"树木",@"飞禽走兽",@"人类",@"动物",nil];
arr2 = [[NSArray alloc]initWithObjects:@"星期一",@"星期二",@"星期三",@"星期四",@"星期五", nil];
//创建选择器控件
picker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 230, 320, 480-230)];
picker.backgroundColor = [UIColor greenColor];
//设置代理
picker.delegate = self;
picker.dataSource = self;
[self.view addSubview: picker];
[self createBtn];
}
//必须实现的协议中的方法
//<1>设置当前的选择器控件的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
//<2>设置每一列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//component指的就是列号,列号的下标从0开始
if (component == 0) {
return [arr1 count];
}
else
{
return [arr2 count];
}
}
//<3>设置每行显示的信息
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//component 表示的是列号
//row表示的是每一列中的行号
if (component == 0) {
return [arr1 objectAtIndex:row];
}
else
{
return [arr2 objectAtIndex:row];
}
}
-(void)createBtn
{
UIButton * btn = [UIButton buttonWithType: UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100, 100);
[btn setTitle:@"完成" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: btn];
}
-(void)pressBtn:(id)sender
{
//获取选中的行号
int indexrow = [picker selectedRowInComponent:0];
//获取选中的内容
NSString * string = [arr1 objectAtIndex:indexrow];
NSString * allstr = [NSString stringWithFormat:@"您选择的是: %@",string];
//使用警告框显示
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:allstr delegate:Nil cancelButtonTitle:@"确定" otherButtonTitles:nil , nil];
[alert show];
[alert release];
}