UIActionSheet,UIAlertView,UISlider,UIProgressView,UISwitch,UISegmentControl,UIActivityIndicatorView基

来源:互联网 发布:linux locate命令 编辑:程序博客网 时间:2024/06/07 20:03
一、UIActionSheet 行动表
UIActionSheet 用来对指定的事件向用户呈现一系列的操作;也可以用来提示用户确认有些带有危险性的操作;ActionSheet包含一个可选的标题和一个或多个按钮,其中每一个对应于要执行的操作
1、声明代理
<UIActionSheetDelegate>

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

实现代理方法

1
2
3
4
5
6
7
8
9
10
11
#pragma mark - UIActionSheetDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(actionSheet.tag == 10) {
        NSLog(@"sheet clicked index = %d",buttonIndex);
    }
    elseif(actionSheet.tag == 100){
        NSLog(@"actionSheet clicked index = %d",buttonIndex);
    }
     
}

二、UIAlertView 警告

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

1
2
3
4
5
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否删除"message:@"删除后不可撤销"delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"确定", nil];
     
alert.tag = 20;
     
[alert show];

1
2
3
4
5
6
7
#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag==20) {
        NSLog(@"you clicked:%@",[alertView buttonTitleAtIndex:buttonIndex]);
    }
}

三、UISlider 滑块

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

1
2
3
4
5
6
7
8
9
10
11
12
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 100, 300, 10)];
     
slider.tag=30;
     
slider.maximumValue = 1;
     
slider.minimumValue = 0;
     
     
[slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
     
[self.view addSubview:slider];

1
2
3
4
5
-(IBAction)sliderValueChanged:(id)sender{
     
    UISlider *slider = (UISlider *)sender;
    self.view.backgroundColor = [UIColor colorWithRed:slider.value green:0.8 blue:0.8 alpha:1];
}

四、UIProgressView 进度条

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

1
2
3
4
5
6
7
8
9
10
11
12
13
// 在.h或是类别中声明progressView,在此初始化
progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 200, 300, 10)];
     
// 如果是放在toolBar上使用UIProgressViewStyleBar
progressView.progressViewStyle = UIProgressViewStyleDefault;
     
// 进度条显示进度部分的颜色
progressView.progressTintColor = [UIColor orangeColor];
     
// 进度条未显示进度部分颜色
progressView.trackTintColor = [UIColor brownColor];
     
[self.view addSubview:progressView];
更改progressView的进度

1
progressView.progress = 0.8;// progress值介于0.0~1.0之间

五、UISwitch开关

你可以使用UISwitch来创建和管理使用开/关按钮,例如,在设置应用程序的选项,比如飞行模式和蓝牙。这些对象被称为开关

1
2
3
4
5
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(20, 150, 50, 20)];
     
[switchView addTarget:self action:@selector(switchOnOrOff:) forControlEvents:UIControlEventValueChanged];
     
[self.view addSubview:switchView];

1
2
3
4
5
6
7
8
9
10
-(IBAction)switchOnOrOff:(id)sender
{
    UISwitch *switchView = (UISwitch *)sender;
    if(switchView.isOn) {
        NSLog(@"on");
    }
    else{
        NSLog(@"off");
    }
}
改变switch状态

1
[switchView setOn:YES animated:YES];

六、UISegmentedControl 分段控制

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

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

1
2
3
4
-(IBAction)changeSegment:(id)sender{
    UISegmentedControl *segment = (UISegmentedControl *)sender;
    NSLog(@"selected %d",segment.selectedSegmentIndex);
}

七.UIActivityIndicatorView 
初始化方法
- initWithActivityIndicatorStyle
控制一个Activity Indicator
- startAnimating
- stopAnimating
- isAnimating
hidesWhenStopped 属性

配置Activity Indicator 外观
activityIndicatorViewStyle 属性
color 属性  (iOS 5  引入)

常量三个
typedef enum { 
UIActivityIndicatorViewStyleWhiteLarge, 
UIActivityIndicatorViewStyleWhite, 
UIActivityIndicatorViewStyleGray,
} UIActivityIndicatorViewStyle; 

使用方式就是
UIActivityIndicatorView *testActivityIndicator = [UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]];
testActivityIndicator.center = CGPointMake(100.0f, 100.0f);//只能设置中心,不能设置大小
[testActivityIndicator setFrame = CGRectMack(100, 100, 100, 100)];//不建议这样设置,因为UIActivityIndicatorView是不能改变大小只能改变位置,这样设置得到的结果是控件的中心在 (100,100)上,而不是和其他控件的frame一样左上角在(100, 100)长为100,宽为100.
[self addSubview:testActivityIndicator];
testActivityIndicator.color = [UIColor redColor]; // 改变圈圈的颜色为红色; iOS5引入
[testActivityIndicator startAnimating]; // 开始旋转
[testActivityIndicator stopAnimating]; // 结束旋转
[testActivityIndicator setHidesWhenStopped:YES]; //当旋转结束时隐藏

还有一个是isAnimating方法,返回一个BOOL值,可以用这个方法来判断控件是否在旋转



initWithActivityIndicatorStyle是UIActivityIndicatorView唯一的初始化方法
属性值是一个枚举变量,只有三个值:
UIActivityIndicatorViewStyleWhite; 白色圆圈
UIActivityIndicatorViewStyleWhiteLarge; 白色圆圈 但是要大些
UIActivityIndicatorViewStyleGray; 灰色圆圈
0 0
原创粉丝点击