IOS8 新增了UIAlertController 类,将原先的两个类 UIAlertView 和 UIActionSheet 封装到新类 UIAlertController 中(即被弃用了)。通过属性新类 UIAlertController 中的UIAlertControllerStyle 属性(UIAlertControllerStyleAlert|UIAlertControllerStyleActionSheet)来区分这两个控件样式的使用。如果需要 AlertView 弹窗样式就将属性设置为UIAlertControllerStyle = UIAlertControllerStyleAlert,如果需要 ActionSheet 样式则用 UIAlertControllerStyle =UIAlertControllerStyleActionSheet 。
前一段时间,项目适配 IOS8 的时候,做了ActionSheet 的适配。因为,我们大多都会将 UIActionSheet 和 UIPickerView 配合使用,即将 UIPickerView 添加到 UIActionSheet 中,自定义一个UIPickerView 的弹窗选择控件。效果如下图:点击视图中的按钮后,从视图底部弹出一个日期选择框(UIPickerView)。将 UIPickerView 添加到 UIActionSheet 中,这样做的目的就是可以刚刚利用原先 UIActionSheet 的底部弹窗效果和附带的灰色蒙板视图。
(由于最近较忙,代码写的和整理的有点乱)
按照原先的对这个封装类 UIMenuSheet 的写法是这样的。
//
// UIMenuSheet.h
// AiCai
//
// Created by on 12-3-29.
// Copyright (c) 2012年 www.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "UIPopMenu.h"
@interface UIMenuSheet : UIActionSheet<UIPickerViewDelegate, UIPickerViewDataSource>
{
UIPickerView *myPickerView;//放在ActionSheet中PickerView
NSArray *pickerViewArray;//PickerView中的选项数据数组,初始化时由外部调用者赋值
id delegateMenu;
}
@property (nonatomic, retain) UIPickerView *myPickerView;
@property (nonatomic, retain) NSArray *pickerViewArray;
@property (nonatomic, assign) id<UIPopMenuDelegate> delegateMenu;
//初始化方法
-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title;
@end
//
// UIMenuSheet.m
// AiCai
//
// Created by on 12-3-29.
// Copyright (c) 2012年 www.com. All rights reserved.
//
#import "UIMenuSheet.h"
@implementation UIMenuSheet
@synthesize myPickerView;
@synthesize pickerViewArray;
@synthesize delegateMenu;
-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title
{
self = [super init];
if (self)
{
int theight = height - 40;
int btnnum = theight/50;
for(int i=0; i<btnnum; i++)
{
[self addButtonWithTitle:@" "];
}
UIView *view= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, height)];
view.backgroundColor = [UIColor whiteColor];
[self addSubview:view];
[view release];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"c_select_time_bg.png"]];
imageView.frame = CGRectMake(0, 0, 320, 44);
[self addSubview:imageView];
[imageView release];
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, height-44)];
myPickerView.showsSelectionIndicator = YES;
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.backgroundColor = [UIColor whiteColor];
[self addSubview:myPickerView];
UIButton *btnCancel=[UIButton buttonWithType:UIButtonTypeCustom];
[btnCancel setFrame:CGRectMake(20, 4, 76, 35)];
[btnCancel setTitle:@"取消" forState:UIControlStateNormal];
[btnCancel setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//btnHelp.backgroundColor=[UIColor clearColor];
// [btnCancel setBackgroundImage:[UIImage imageNamed:@"c_select_time_btn.png"] forState:UIControlStateNormal];
[btnCancel addTarget:self action:@selector(OnCancel) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btnCancel];
UIButton *btnOK=[UIButton buttonWithType:UIButtonTypeCustom];
[btnOK setFrame:CGRectMake(224, 4, 76, 35)];
[btnOK setTitle:@"确定" forState:UIControlStateNormal];
[btnOK setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
// [btnOK setBackgroundImage:[UIImage imageNamed:@"c_select_time_btn.png"] forState:UIControlStateNormal];
[btnOK addTarget:self action:@selector(OnOK) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btnOK];
}
return self;
}
-(void) OnOK
{
int selectRow = [myPickerView selectedRowInComponent:0];
//[pickerViewArray objectAtIndex:selectRow];
if ([delegateMenu respondsToSelector:@selector(didSelectRow:string:)]) {
[delegateMenu didSelectRow:selectRow string: [pickerViewArray objectAtIndex:selectRow]];
}
[self dismissWithClickedButtonIndex:selectRow animated:YES];
}
-(void) OnCancel
{
if ([delegateMenu respondsToSelector:@selector(cancelSelectRow)]) {
[delegateMenu cancelSelectRow];
}
[self dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)dealloc
{
[myPickerView release];
[pickerViewArray release];
[super dealloc];
}
#pragma mark -
#pragma mark UIPickerViewDataSource
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerViewArray objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerViewArray count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
@end
但是在ios8 下,这样做是看不到效果的。于是,又在这个 UIMenuSheet 类中加入适配ios 8 的新类 UIMenuSheet_I8 。 新类的代码如下:
//
// UIMenuSheet.h
// AiCai
//
// Created by on 12-3-29.
// Copyright (c) 2012年 www.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "UIPopMenu.h"
@interface UIMenuSheet : UIActionSheet<UIPickerViewDelegate, UIPickerViewDataSource>
{
UIPickerView *myPickerView;
NSArray *pickerViewArray;
id delegateMenu;
}
@property (nonatomic, retain) UIPickerView *myPickerView;
@property (nonatomic, retain) NSArray *pickerViewArray;
@property (nonatomic, assign) id<UIPopMenuDelegate> delegateMenu;
-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title;
@end
//适配ios8 UIAlertViewController
@interface UIMenuSheet_I8 : UIAlertController<UIPickerViewDelegate, UIPickerViewDataSource>
{
UIPickerView *myPickerView;
NSArray *pickerViewArray;
id delegateMenu;
}
@property (nonatomic, retain) UIPickerView *myPickerView;
@property (nonatomic, retain) NSArray *pickerViewArray;
@property (nonatomic, assign) id<UIPopMenuDelegate> delegateMenu;
-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title;
@end
//
// UIMenuSheet.m
// AiCai
//
// Created by on 12-3-29.
// Copyright (c) 2012年 www.AiCai.com. All rights reserved.
//
#import "UIMenuSheet.h"
@implementation UIMenuSheet
@synthesize myPickerView;
@synthesize pickerViewArray;
@synthesize delegateMenu;
-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title
{
self = [super init];
if (self)
{
int theight = height - 40;
int btnnum = theight/50;
for(int i=0; i<btnnum; i++)
{
[self addButtonWithTitle:@" "];
}
UIView *view= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, height)];
view.backgroundColor = [UIColor whiteColor];
[self addSubview:view];
[view release];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"c_select_time_bg.png"]];
imageView.frame = CGRectMake(0, 0, 320, 44);
[self addSubview:imageView];
[imageView release];
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, height-44)];
myPickerView.showsSelectionIndicator = YES;
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.backgroundColor = [UIColor whiteColor];
[self addSubview:myPickerView];
UIButton *btnCancel=[UIButton buttonWithType:UIButtonTypeCustom];
[btnCancel setFrame:CGRectMake(20, 4, 76, 35)];
[btnCancel setTitle:@"取消" forState:UIControlStateNormal];
[btnCancel setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//btnHelp.backgroundColor=[UIColor clearColor];
// [btnCancel setBackgroundImage:[UIImage imageNamed:@"c_select_time_btn.png"] forState:UIControlStateNormal];
[btnCancel addTarget:self action:@selector(OnCancel) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btnCancel];
UIButton *btnOK=[UIButton buttonWithType:UIButtonTypeCustom];
[btnOK setFrame:CGRectMake(224, 4, 76, 35)];
[btnOK setTitle:@"确定" forState:UIControlStateNormal];
[btnOK setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
// [btnOK setBackgroundImage:[UIImage imageNamed:@"c_select_time_btn.png"] forState:UIControlStateNormal];
[btnOK addTarget:self action:@selector(OnOK) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btnOK];
}
return self;
}
-(void) OnOK
{
int selectRow = [myPickerView selectedRowInComponent:0];
//[pickerViewArray objectAtIndex:selectRow];
if ([delegateMenu respondsToSelector:@selector(didSelectRow:string:)]) {
[delegateMenu didSelectRow:selectRow string: [pickerViewArray objectAtIndex:selectRow]];
}
[self dismissWithClickedButtonIndex:selectRow animated:YES];
}
-(void) OnCancel
{
if ([delegateMenu respondsToSelector:@selector(cancelSelectRow)]) {
[delegateMenu cancelSelectRow];
}
[self dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)dealloc
{
[myPickerView release];
[pickerViewArray release];
[super dealloc];
}
#pragma mark -
#pragma mark UIPickerViewDataSource
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerViewArray objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerViewArray count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
@end
@implementation UIMenuSheet_I8
@synthesize myPickerView;
@synthesize pickerViewArray;
@synthesize delegateMenu;
-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title
{
self = [super init];
if (self)
{
int theight = height - 40;
int btnnum = theight/50;
for(int i=0; i<btnnum; i++)
{
//这里的 UIAlertAction 相当于原先 UIActionSheet 中调用 addButtonWithTitle: 方法中的占位 Button。UIAlertAction使用block语法
UIAlertAction *action = [UIAlertAction actionWithTitle:@"1" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
}];
[self addAction:action];
}
UIView *view= [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-16, height)];
view.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self.view addSubview:view];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"c_select_time_bg.png"]];
imageView.frame = CGRectMake(0, 0, self.view.frame.size.width-16, 44);
[self.view addSubview:imageView];
[imageView release];
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 272, height-44)];
myPickerView.showsSelectionIndicator = YES;
myPickerView.delegate = self;
myPickerView.dataSource = self;
[self.view addSubview:myPickerView];
UIButton *btnCancel=[UIButton buttonWithType:UIButtonTypeCustom];
[btnCancel setFrame:CGRectMake(10, 4, 76, 35)];
[btnCancel setTitle:@"取消" forState:UIControlStateNormal];
[btnCancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//btnHelp.backgroundColor=[UIColor clearColor];
[btnCancel setBackgroundImage:[UIImage imageNamed:@"c_select_time_btn.png"] forState:UIControlStateNormal];
[btnCancel addTarget:self action:@selector(OnCancel) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnCancel];
UIButton *btnOK=[UIButton buttonWithType:UIButtonTypeCustom];
[btnOK setFrame:CGRectMake(220, 4, 76, 35)];
[btnOK setTitle:@"确定" forState:UIControlStateNormal];
[btnOK setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnOK setBackgroundImage:[UIImage imageNamed:@"c_select_time_btn.png"] forState:UIControlStateNormal];
[btnOK addTarget:self action:@selector(OnOK) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnOK];
}
return self;
}
-(void) OnOK
{
int selectRow = [myPickerView selectedRowInComponent:0];
//[pickerViewArray objectAtIndex:selectRow];
if ([delegateMenu respondsToSelector:@selector(didSelectRow:string:)]) {
[delegateMenu didSelectRow:selectRow string: [pickerViewArray objectAtIndex:selectRow]];
}
//[self dismissWithClickedButtonIndex:selectRow animated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void) OnCancel
{
//[self dismissWithClickedButtonIndex:0 animated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)dealloc
{
[myPickerView release];
[pickerViewArray release];
[super dealloc];
}
#pragma mark -
#pragma mark UIPickerViewDataSource
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerViewArray objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerViewArray count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
@end
使用的时候,当点击按钮的时候,只要判断当前系统的版本号,然后加载对应的类就可以实现原先的效果了。
-(IBAction) chooseTime{
//改变按钮背景图片
if(IOS_VERSION<8.0){
UIMenuSheet* sheet = [[UIMenuSheet alloc] initWithHeight:284.0f WithSheetTitle:@"选择查询期限"];
sheet.pickerViewArray = [NSArray arrayWithObjects:@"今天",@"一周",@"一个月",@"三个月",nil];
sheet.delegateMenu = self;
//iOS8 之前使用 UIActionSheet 中的 showInView 方法展现视图
[sheet showInView:self.view];
[sheet release];
}
else
{
UIMenuSheet_I8* sheet = [[UIMenuSheet_I8 alloc] initWithHeight:284.0f WithSheetTitle:@"选择查询期限"];
sheet.pickerViewArray = [NSArray arrayWithObjects:@"今天",@"一周",@"一个月",@"三个月",nil];
sheet.delegateMenu = self;
//iOS8 之后使用 presentViewController 方法推出视图
[self presentViewController:sheet animated:YES completion:nil];
[sheet release];
}
}