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];
实现代理方法
#pragma mark - UIActionSheetDelegate -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (actionSheet.tag == 10) { NSLog(@"sheet clicked index = %d",buttonIndex); } else if(actionSheet.tag == 100){ NSLog(@"actionSheet clicked index = %d",buttonIndex); } }
使用UIAlertView向用户提示一条警告消息,Alert View功能与ActionSheet类似,但是外观上有所不同(UIActionSheet的一个实例)
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否删除" message:@"删除后不可撤销" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; alert.tag = 20; [alert show];
#pragma mark - UIAlertViewDelegate -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag==20) { NSLog(@"you clicked:%@",[alertView buttonTitleAtIndex:buttonIndex]); } }
UISlider对象是一个用来显示从一系列的值中选择一个单一的值的可视化控制器。滑块始终为横条,一个指示器用来标注这个滑块的当前值,并且可以让用户移动来更改设置值。
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];
-(IBAction)sliderValueChanged:(id)sender{ UISlider *slider = (UISlider *)sender; self.view.backgroundColor = [UIColor colorWithRed:slider.value green:0.8 blue:0.8 alpha:1]; }
你可以使用UIProgressView类来描绘一段时间内某任务的进度。使用进度条的一个例子就是你在邮件中下载相关信息时显示在底部的东西
// 在.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的进度
progressView.progress = 0.8;// progress值介于0.0~1.0之间
你可以使用UISwitch来创建和管理使用开/关按钮,例如,在设置应用程序的选项,比如飞行模式和蓝牙。这些对象被称为开关
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(20, 150, 50, 20)]; [switchView addTarget:self action:@selector(switchOnOrOff:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:switchView];
-(IBAction)switchOnOrOff:(id)sender { UISwitch *switchView = (UISwitch *)sender; if (switchView.isOn) { NSLog(@"on"); } else { NSLog(@"off"); } }改变switch状态
[switchView setOn:YES animated:YES];
UISegmentedControl对象是由多个段组成的水平控制器,每个段作为一个独立的按钮功能。UISegmentControl提供了一个紧凑的方式组合到一起的多个控件。
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];
-(IBAction)changeSegment:(id)sender{ UISegmentedControl *segment = (UISegmentedControl *)sender; NSLog(@"selected %d",segment.selectedSegmentIndex); }