您的位置:首页 > 产品设计 > UI/UE

UIActionSheet,UIAlertView等UI控件用法

2013-12-20 10:03 399 查看

一、UIActionSheet 行动表

UIActionSheet 用来对指定的事件向用户呈现一系列的操作;也可以用来提示用户确认有些带有危险性的操作;ActionSheet包含一个可选的标题和一个或多个按钮,其中每一个对应于要执行的操作
1、声明代理

1
<UIActionSheetDelegate>
2、初始化加载

01
//
 简单初始化显示
02
UIActionSheet
 *sheet = [[UIActionSheet alloc] initWithTitle:@
"选择操作"
delegate:self
 cancelButtonTitle:nil destructiveButtonTitle:@
"删除"
otherButtonTitles:@
"取消"
,
 nil];
03
 
04
//
 用tag值来分辨是代理方法调用时哪个actionSheet
05
sheet.tag
 = 10;
06
 
07
[sheet
 showInView:self.view];
08
 
09
 
10
//
 按钮比较多时,将按钮标题放在数组中遍历插入
11
UIActionSheet
 *actionSheet = [[UIActionSheet alloc] initWithTitle:@
"选择操作"
delegate:self
 cancelButtonTitle:@
"取消"
destructiveButtonTitle:nil
 otherButtonTitles: nil];
12
 
13
actionSheet.tag
 = 100;
14
 
15
for
(NSString
 *title in array){
16
[actionSheet
 addButtonWithTitle:title];
17
}
18
[actionSheet
 showInView:self.view];
19
 
20
 
21
//
 在iPad上显示带箭头的actionSheet,在iPhone上只能从底部弹出,如需在iPhone上出现带箭头的形势
22
//
 从某个位置弹出
23
[actionSheet
 showFromRect:CGRectMake(20, 30, 100, 100) inView:self.view animated:YES];
24
 
25
//
 从导航栏右键弹出
26
[actionSheet
 showFromBarButtonItem:self.navigationController.navigationItem.rightBarButtonItem animated:YES];
实现代理方法

01
#pragma
 mark - UIActionSheetDelegate
02
-(
void
)actionSheet:(UIActionSheet
 *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
03
{
04
if
(actionSheet.tag
 == 10) {
05
NSLog(@
"sheet
 clicked index = %d"
,buttonIndex);
06
}
07
else
if
(actionSheet.tag
 == 100){
08
NSLog(@
"actionSheet
 clicked index = %d"
,buttonIndex);
09
}
10
 
11
}

二、UIAlertView 警告

使用UIAlertView向用户提示一条警告消息,Alert View功能与ActionSheet类似,但是外观上有所不同(UIActionSheet的一个实例)

1
UIAlertView
 *alert = [[UIAlertView alloc] initWithTitle:@
"是否删除"
message:@
"删除后不可撤销"
delegate:self
 cancelButtonTitle:@
"取消"
otherButtonTitles:@
"确定"
,
 nil];
2
 
3
alert.tag
 = 20;
4
 
5
[alert
 show];
1
#pragma
 mark - UIAlertViewDelegate
2
-(
void
)alertView:(UIAlertView
 *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
3
{
4
if
(alertView.tag==20)
 {
5
NSLog(@
"you
 clicked:%@"
,[alertView
 buttonTitleAtIndex:buttonIndex]);
6
}
7
}

三、UISlider 滑块

UISlider对象是一个用来显示从一系列的值中选择一个单一的值的可视化控制器。滑块始终为横条,一个指示器用来标注这个滑块的当前值,并且可以让用户移动来更改设置值。

01
UISlider
 *slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 100, 300, 10)];
02
 
03
slider.tag=30;
04
 
05
slider.maximumValue
 = 1;
06
 
07
slider.minimumValue
 = 0;
08
 
09
 
10
[slider
 addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
11
 
12
[self.view
 addSubview:slider];
1
-(IBAction)sliderValueChanged:(id)sender{
2
 
3
UISlider
 *slider = (UISlider *)sender;
4
self.view.backgroundColor
 = [UIColor colorWithRed:slider.value green:0.8 blue:0.8 alpha:1];
5
}

四、UIProgressView 进度条

你可以使用UIProgressView类来描绘一段时间内某任务的进度。使用进度条的一个例子就是你在邮件中下载相关信息时显示在底部的东西

01
//
 在.h或是类别中声明progressView,在此初始化
02
progressView
 = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 200, 300, 10)];
03
 
04
//
 如果是放在toolBar上使用UIProgressViewStyleBar
05
progressView.progressViewStyle
 = UIProgressViewStyleDefault;
06
 
07
//
 进度条显示进度部分的颜色
08
progressView.progressTintColor
 = [UIColor orangeColor];
09
 
10
//
 进度条未显示进度部分颜色
11
progressView.trackTintColor
 = [UIColor brownColor];
12
 
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)];
2
 
3
[switchView
 addTarget:self action:@selector(switchOnOrOff:) forControlEvents:UIControlEventValueChanged];
4
 
5
[self.view
 addSubview:switchView];
01
-(IBAction)switchOnOrOff:(id)sender
02
{
03
UISwitch
 *switchView = (UISwitch *)sender;
04
if
(switchView.isOn)
 {
05
NSLog(@
"on"
);
06
}
07
else
{
08
NSLog(@
"off"
);
09
}
10
}
改变switch状态

1
[switchView
 setOn:YES animated:YES];

六、UISegmentedControl 分段控制

UISegmentedControl对象是由多个段组成的水平控制器,每个段作为一个独立的按钮功能。UISegmentControl提供了一个紧凑的方式组合到一起的多个控件。

1
UISegmentedControl
 *segment = [[UISegmentedControl alloc] initWithFrame:CGRectMake(50, 50, 50, 44)];
2
 
3
[segment
 insertSegmentWithTitle:@
"男"
atIndex:0
 animated:YES];
4
 
5
[segment
 insertSegmentWithTitle:@
"女"
atIndex:1
 animated:YES];
6
 
7
[segment
 addTarget:self action:@selector(changeSegment:) forControlEvents:UIControlEventValueChanged];
8
9
[self.view
 addSubview:segment];
1
-(IBAction)changeSegment:(id)sender{
2
UISegmentedControl
 *segment = (UISegmentedControl *)sender;
3
NSLog(@
"selected
 %d"
,segment.selectedSegmentIndex);
4
}


UIActionSheet,UIAlertView等UI控件用法

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: