iOSのピッカーを簡単に扱えるActionSheetPicker
久しぶりのiOSの開発ネタです。
設定画面でUIPickerを使ってみようと思ったのですが、 意外と面倒だったのでライブラリを探ました。
ActionSheetPickerというライブラリが中々良かったので紹介します。
やりたかったこと
- UITableViewCellをタップして、UIPickerを表示する。
- UIPickerで項目を選択して閉じる
できること
サンプルコード
// Inside a IBAction method: // Create an array of strings you want to show in the picker: NSArray *colors = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue", @"Orange", nil]; [ActionSheetStringPicker showPickerWithTitle:@"Select a Color" rows:colors initialSelection:0 doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) { NSLog(@"Picker: %@, Index: %@, value: %@", picker, selectedIndex, selectedValue); } cancelBlock:^(ActionSheetStringPicker *picker) { NSLog(@"Block Picker Canceled"); } origin:sender]; // You can also use self.view if you don't have a sender
githubのコードそのままですが、これだけで簡単に実装できます。