一、UIActionSheet 行动表
UIActionSheet 用来对指定的事件向用户呈现一系列的操作;也可以用来提示用户确认有些带有危险性的操作;ActionSheet包含一个可选的标题和一个或多个按钮,其中每一个对应于要执行的操作
1、声明代理
2、初始化加载
02 | UIActionSheet
*sheet = [[UIActionSheet alloc] initWithTitle:@ "选择操作" delegate:self
cancelButtonTitle:nil destructiveButtonTitle:@ "删除" otherButtonTitles:@ "取消" ,
nil]; |
04 | //
用tag值来分辨是代理方法调用时哪个actionSheet |
07 | [sheet
showInView:self.view]; |
10 | //
按钮比较多时,将按钮标题放在数组中遍历插入 |
11 | UIActionSheet
*actionSheet = [[UIActionSheet alloc] initWithTitle:@ "选择操作" delegate:self
cancelButtonTitle:@ "取消" destructiveButtonTitle:nil
otherButtonTitles: nil]; |
15 | for (NSString
*title in array){ |
16 | [actionSheet
addButtonWithTitle:title]; |
18 | [actionSheet
showInView:self.view]; |
21 | //
在iPad上显示带箭头的actionSheet,在iPhone上只能从底部弹出,如需在iPhone上出现带箭头的形势 |
23 | [actionSheet
showFromRect:CGRectMake(20, 30, 100, 100) inView:self.view animated:YES]; |
26 | [actionSheet
showFromBarButtonItem:self.navigationController.navigationItem.rightBarButtonItem animated:YES]; |
实现代理方法
01 | #pragma
mark - UIActionSheetDelegate |
02 | -( void )actionSheet:(UIActionSheet
*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex |
04 | if (actionSheet.tag
== 10) { |
05 | NSLog(@ "sheet
clicked index = %d" ,buttonIndex); |
07 | else if (actionSheet.tag
== 100){ |
08 | NSLog(@ "actionSheet
clicked index = %d" ,buttonIndex); |
二、UIAlertView 警告
使用UIAlertView向用户提示一条警告消息,Alert View功能与ActionSheet类似,但是外观上有所不同(UIActionSheet的一个实例)
1 | UIAlertView
*alert = [[UIAlertView alloc] initWithTitle:@ "是否删除" message:@ "删除后不可撤销" delegate:self
cancelButtonTitle:@ "取消" otherButtonTitles:@ "确定" ,
nil]; |
1 | #pragma
mark - UIAlertViewDelegate |
2 | -( void )alertView:(UIAlertView
*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex |
4 | if (alertView.tag==20)
{ |
5 | NSLog(@ "you
clicked:%@" ,[alertView
buttonTitleAtIndex:buttonIndex]); |
三、UISlider 滑块
UISlider对象是一个用来显示从一系列的值中选择一个单一的值的可视化控制器。滑块始终为横条,一个指示器用来标注这个滑块的当前值,并且可以让用户移动来更改设置值。
01 | UISlider
*slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 100, 300, 10)]; |
05 | slider.maximumValue
= 1; |
07 | slider.minimumValue
= 0; |
10 | [slider
addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged]; |
12 | [self.view
addSubview:slider]; |
1 | -(IBAction)sliderValueChanged:(id)sender{ |
3 | UISlider
*slider = (UISlider *)sender; |
4 | self.view.backgroundColor
= [UIColor colorWithRed:slider.value green:0.8 blue:0.8 alpha:1]; |
四、UIProgressView 进度条
你可以使用UIProgressView类来描绘一段时间内某任务的进度。使用进度条的一个例子就是你在邮件中下载相关信息时显示在底部的东西
01 | //
在.h或是类别中声明progressView,在此初始化 |
02 | progressView
= [[UIProgressView alloc] initWithFrame:CGRectMake(20, 200, 300, 10)]; |
04 | //
如果是放在toolBar上使用UIProgressViewStyleBar |
05 | progressView.progressViewStyle
= UIProgressViewStyleDefault; |
08 | progressView.progressTintColor
= [UIColor orangeColor]; |
11 | progressView.trackTintColor
= [UIColor brownColor]; |
13 | [self.view
addSubview:progressView]; |
更改progressView的进度
1 | progressView.progress
= 0.8; //
progress值介于0.0~1.0之间 |
五、UISwitch开关
你可以使用UISwitch来创建和管理使用开/关按钮,例如,在设置应用程序的选项,比如飞行模式和蓝牙。这些对象被称为开关
1 | UISwitch
*switchView = [[UISwitch alloc] initWithFrame:CGRectMake(20, 150, 50, 20)]; |
3 | [switchView
addTarget:self action:@selector(switchOnOrOff:) forControlEvents:UIControlEventValueChanged]; |
5 | [self.view
addSubview:switchView]; |
01 | -(IBAction)switchOnOrOff:(id)sender |
03 | UISwitch
*switchView = (UISwitch *)sender; |
改变switch状态
1 | [switchView
setOn:YES animated:YES]; |
六、UISegmentedControl 分段控制
UISegmentedControl对象是由多个段组成的水平控制器,每个段作为一个独立的按钮功能。UISegmentControl提供了一个紧凑的方式组合到一起的多个控件。
1 | UISegmentedControl
*segment = [[UISegmentedControl alloc] initWithFrame:CGRectMake(50, 50, 50, 44)]; |
3 | [segment
insertSegmentWithTitle:@ "男" atIndex:0
animated:YES]; |
5 | [segment
insertSegmentWithTitle:@ "女" atIndex:1
animated:YES]; |
7 | [segment
addTarget:self action:@selector(changeSegment:) forControlEvents:UIControlEventValueChanged]; |
9 | [self.view
addSubview:segment]; |
1 | -(IBAction)changeSegment:(id)sender{ |
2 | UISegmentedControl
*segment = (UISegmentedControl *)sender; |
3 | NSLog(@ "selected
%d" ,segment.selectedSegmentIndex); |
UIActionSheet,UIAlertView等UI控件用法