iPhone开发自定义视图之ActionSheet中使用PickerView .

实现的功能:1)打开ActionSheet后展示PickerView,进行选择操作。

关键词:ActionSheet PickerView


1、新建一个Sigle View Application,命名为PickerInActionSheet,工程结构如下:

[img]
[img]http://dl.iteye.com/upload/attachment/0078/7390/b9bccfb3-f501-337a-9562-a4b8d42ae607.png[/img]
[/img]


2、修改ViewController.xib,添加一个TextField控件。


3、修改ViewController.h,如下:
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>

@property(retain,nonatomic)UIPickerView *picker;
@property(retain,nonatomic)UIActionSheet *actionSheet;
@property(retain,nonatomic)IBOutlet UITextField *textField;

@property(nonatomic,retain)NSDictionary *appleDevices;
@property(nonatomic,retain)NSArray *deviceCategory;
@property(nonatomic,retain)NSArray *deviceName;

-(IBAction)showActionSheet:(id)sender;
@end


连接输出口textField及操作showActionSheet,如下:
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7392/19ff8a7c-64f9-3d33-a5fe-ac6c69e989b0.png[/img]
[/img]


4、修改ViewController.m,如下:

#import "ViewController.h"

#define kDeviceCategory 0
#define kDeviceName 1

@interface ViewController ()

@end

@implementation ViewController
@synthesize appleDevices;
@synthesize deviceCategory;
@synthesize deviceName;
@synthesize picker;
@synthesize actionSheet;
@synthesize textField;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initData];
}

//初始化数据
-(void)initData{
textField.placeholder = @"请点击。。。";
textField.textAlignment = UITextAlignmentCenter;

NSArray *array1 = [NSArray arrayWithObjects:@"iPhone",@"iPad",@"iPod",nil];
NSArray *array2 = [NSArray arrayWithObjects:@"Mac",@"iMac",@"Mac Mini",@"Mac Pro",nil];
NSDictionary *dictionary= [NSDictionary dictionaryWithObjectsAndKeys:array1,@"Mobile",array2,@"Computers",nil];
appleDevices = [[NSDictionary alloc]initWithDictionary:dictionary copyItems:YES];

NSArray *components = [self.appleDevices allKeys];
NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
self.deviceCategory = sorted;

NSString *selectedCategory = [self.deviceCategory objectAtIndex:0];
self.deviceName = [self.appleDevices objectForKey:selectedCategory];
}

//初始化视图
-(void)showActionSheetPicker{
//在title中加入多个换行,给picker留出空间,否则picker会盖住ActionSheet的button
NSString *title = @"请选择设备\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil,nil];

picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 320, 220)];
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;

[actionSheet addSubview:picker];
[actionSheet showInView:self.view];
}

-(void)dealloc{
self.deviceName = nil;
self.deviceCategory = nil;
self.appleDevices = nil;
self.picker = nil;
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

//显示actionSheet
-(IBAction)showActionSheet:(id)sender{
[self showActionSheetPicker];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//强制用竖屏模式
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}


#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(component == kDeviceCategory){
return [self.deviceCategory count];
}else{
return [self.deviceName count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(component == kDeviceCategory){
return [self.deviceCategory objectAtIndex:row];
}else{
return [self.deviceName objectAtIndex:row];
}
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
if(component==kDeviceCategory){
return 150;
}else{
return 170;
}
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 35;
}

//显示picker中的数据
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSString *inputString;
if(component == kDeviceCategory){
NSString *selectedCategory = [self.deviceCategory objectAtIndex:row];
NSArray *array = [self.appleDevices objectForKey:selectedCategory];
self.deviceName = array;
[self.picker selectRow:0 inComponent:kDeviceName animated:YES];
[self.picker reloadComponent:kDeviceName];

inputString = selectedCategory;
}else if(component == kDeviceName){
NSUInteger selectedCategoryRow = [pickerView selectedRowInComponent:kDeviceCategory];
NSString *selectedCategory = [self.deviceCategory objectAtIndex:selectedCategoryRow];

inputString = [NSString stringWithFormat:@"%@-%@",selectedCategory,[self.deviceName objectAtIndex:row]];
}
//给文本框设置值
self.textField.text=inputString;
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end



5、运行效果如下:
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7394/4897f99f-4159-3806-bac3-25d914639184.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7396/195f7e54-f0d5-3fdf-a2c1-f533e7336b03.png[/img]
[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值