iPhone開発【18】カスタムビューのActionSheetでPickerViewを使用
6893 ワード
転載は出典を明記してください.http://blog.csdn.net/m_changgong/article/details/8245964作者:張燕広
実装する機能:1)ActionSheetを開いてPickerViewを表示し,選択操作を行う.
キーワード:ActionSheet PickerView
1、新しいSigle View Applicationを作成し、PickerInActionSheetと命名し、工事構造は以下の通りである.
2、ViewControllerを修正する.xib、TextFieldコントロールを追加します.
3、ViewControllerを修正する.h,以下:
4、ViewControllerを修正する.m,以下:
、運行効果は以下の通りである:
ソースが必要な方はメッセージをお願いします
実装する機能:1)ActionSheetを開いてPickerViewを表示し,選択操作を行う.
キーワード:ActionSheet PickerView
1、新しいSigle View Applicationを作成し、PickerInActionSheetと命名し、工事構造は以下の通りである.
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を接続し、以下のようにする.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 = @"
";
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 、運行効果は以下の通りである:
ソースが必要な方はメッセージをお願いします