iOS | UIPickerViewをUIActionSheet内に出す(かつPickerを複数設定)

2013-08-05

テキストフィールドに入力する時、キーボードじゃなくてピッカーで入力させたいな〜と思ったので、その備忘録。
尚且つ、テキストフィールドごとにピッカーの内容を変えるやり方。
まずはヘッダーファイル。
[objc]
ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate, UIActionSheetDelegate, UIPickerViewDelegate, UIPickerViewDataSource> {

UIActionSheet *actionSheet;
}

@property (weak, nonatomic) IBOutlet UITextField *textField1; //StoryBoardでOutlet接続
@property (weak, nonatomic) IBOutlet UITextField *textField2; //StoryBoardでOutlet接続

@end
[/objc]
次はソースファイル。
[objc]
ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
UIPickerView *picker1;
UIPickerView *picker2;

NSString *pic1_str;
NSString *pic2_str;
}
@synthesize textField1;
@synthesize textField2;
– (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
textField1.delegate = self;
textField2.delegate = self;
textField1.tag = 1;
textField2.tag = 2;

CGRect rect = [[UIScreen mainScreen] bounds];

picker1 = [[UIPickerView alloc] init];
picker1.frame = CGRectMake(0, rect.size.height, 320, 216);
picker1.showsSelectionIndicator = YES;
picker1.delegate = self;
picker1.dataSource = self;
picker1.tag = 1;
[self.view addSubview:picker1];

picker2 = [[UIPickerView alloc] init];
picker2.frame = CGRectMake(0, rect.size.height, 320,216);
picker2.showsSelectionIndicator = YES;
picker2.delegate = self;
picker2.dataSource = self;
picker2.tag = 2;
[self.view addSubview:picker2];
}

– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
– (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
CGRect rect = [[UIScreen mainScreen] bounds];
if (textField.tag == 1) {
NSLog(@"1");
picker2.frame = CGRectMake(0, rect.size.height, 320, 216);
[self showPicker1]; //picker1を表示させるメソッドを実行
//キーボードは表示させない
return NO;
}
if (textField.tag == 2) {
NSLog(@"2");
picker1.frame = CGRectMake(0, rect.size.height, 320, 216);
[self showPicker2]; //picker2を表示させるメソッドを実行
//キーボードは表示させない
return NO;
}
return NO;
}

– (void)showPicker1 {
// アクションシートの作成
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
// ツールバーの作成
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
[toolBar sizeToFit];
// ピッカーの作成
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.tag = 1;
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
// Cancelボタンの作成
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelDidPush)];
// フレキシブルスペースの作成
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
// Doneボタンの作成
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneDidPush)];
NSArray *items = [NSArray arrayWithObjects:cancel, spacer, done, nil];
[toolBar setItems:items animated:YES];
// アクションシートへの埋め込みと表示
[actionSheet addSubview:toolBar];
[actionSheet addSubview:pickerView];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 464)];
}
– (void)showPicker2 {
// アクションシートの作成
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
// ツールバーの作成
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
[toolBar sizeToFit];
// ピッカーの作成
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.tag = 2;
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
// Cancelボタンの作成
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelDidPush)];
// フレキシブルスペースの作成
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
// Doneボタンの作成
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneDidPush)];
NSArray *items = [NSArray arrayWithObjects:cancel, spacer, done, nil];
[toolBar setItems:items animated:YES];
// アクションシートへの埋め込みと表示
[actionSheet addSubview:toolBar];
[actionSheet addSubview:pickerView];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 464)];
}

– (void)cancelDidPush {
// アクションシートの非表示
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

– (void)doneDidPush {
/* 処理 */
// アクションシートの非表示
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

– (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker {

// ピッカーの列数
return 1;
}

– (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// ピッカーの行数
if (pickerView.tag == 1)
{
return 5;
}
else if (pickerView.tag == 2)
{
return 4;
}
return 0;
}

– (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (pickerView.tag==1)
{
if (component == 0) {
NSString *album[] = {@"1", @"2", @"3", @"4", @"5"};
return album[row];
}
else {
return @"";
}
}
else if (pickerView.tag==2)
{
if (component == 0) {
NSString *album[] = {@"A", @"B", @"C", @"D"};
return album[row];
}
else {
return @"";
}
}
return @"";
}
– (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView.tag == 1) {
NSArray *arr = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
pic1_str = [NSString stringWithFormat:@"%@",[arr objectAtIndex:row]];
textField1.text = pic1_str;
}
if (pickerView.tag == 2) {
NSArray *arr = [[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",nil];
pic2_str = [NSString stringWithFormat:@"%@",[arr objectAtIndex:row]];
textField2.text = pic2_str;
}
}
@end
[/objc]

Copyright(c) 2015 AndByYOU INC. All Rights Reserved.