一个可轻松使用和自定义的视图组件,它提供了一个带有工具栏、完成按钮、动画、设计选项、选定值和先前选定值功能性的 UIPickerView
。适用于iOS 6,在iOS 7中表现更好,因为它允许您更改PickerView的背景颜色。
注意: 如果您的项目没有使用ARC,必须在目标设置 > 编译阶段 > 编译源中添加编译器标志 -fobjc-arc
到 MMPickerView.m
MMPickerView/MMPickerView
文件夹拖放到您的项目中。#import "MMPickerView.h"
来包含MMPickerView。(在 /Demo
中查看示例Xcode项目)
MMPickerView作为一个单例来创建(即不需要显式分配和实例化;您可以直接调用 [MMPickerView method]
)。
您可以显示PickerView
+(void)showPickerViewInView: (UIView *)view
withStrings: (NSArray *)strings
withOptions: (NSDictionary *)options
completion: (void(^)(NSString *selectedString))completion;
+(void)showPickerViewInView: (UIView *)view
withObjetcs: (NSArray *)objects
withOptions: (NSDictionary *)options
objectToStringConverter: (NSString *(^)(id object))converter
completion: (void(^)(id selectedObject))completion;
NSArray *strings = @[@"This", @"is", @"just", @"an array", @"of strings."];
[MMPickerView showPickerViewInView:self.view
withStrings:strings
withOptions:nil
completion:^(NSString *selectedString) {
//selectedString is the return value which you can use as you wish
self.label.text = selectedString;
}];
NSArray *objects = @[@"This is a mix of objects", @14, @13.3, @"A string", @1000];
[MMPickerView showPickerViewInView:self.view
withObjects:objects
withOptions:nil
objectToStringConverter:^NSString *(id object) {
//This is where you convert your object and return a string, for eg. return person.name;
return [object description];
}
completion:^(id selectedObject) {
//The selected object is returned, and you can use the value as you wish
//For example: self.label.text = person.name;
self.label.text = [selectedObject description];
}];
两种展示方法都使用一个 NSDictionary
来设置 MMPickerView
的选项。如果你想得到一个看起来更原始的 PickerView,只需使用 withOptions: nil
。在定制的情况下,可以使用不同的属性中的任何一个来自定义 PickerView。所有属性都是可选的,这意味着如果你想只更改一项,例如文本颜色,你可以这样做,withOptions: @{MMtextColor: [UIColor redColor]}
选项
MMbackgroundColor
- UIColor
MMtextColor
- UIColor
MMtoolbarBackgroundColor
- UIColor
MMbuttonColor
- UIColor
MMfont
- UIFont
MMValueY
- NSInteger
MMselectedObject
- id
MMtoolbarBackgroundImage
- UIImage
MMtextAlignment
- NSNumber
/*
Options:
MMbackgroundColor - UIColor - The background color of the PickerView (>=iOS 7)
MMtextColor - UIColor - The text color of the PickerView
MMtoolbarBackgroundColor - UIColor - The background color of the toolbar
MMbuttonColor - UIColor - The background color (<= iOS 6) or text color (>=iOS 7) of the Done button
MMfont - UIFont - The font of the PickerView labels
MMValueY - NSInteger - The Y value from the top of every label in the PickerView, useful when changing font/font-size.
MMselectedObject - id - The selected object presented in the PickerView, an object from the array, for eg. [yourArray objectAtIndex:0];
MMtoolbarBackgroundImage - UIImage - The background image of the toolbar (320 x 44 for non retina, 640 x 88 for retina)
MMtextAlignment - NSNumber - The text alignment of the labels in the PickerView, @0 for Left, @1 for Center, @2 for Right
*/
NSArray *strings = @[@"This", @"is", @"just", @"an array", @"of strings."];
UIFont *customFont = [UIFont fontWithName:@"Palatino-Bold" size:19.0];
NSDictionary *options = @{MMbackgroundColor: [UIColor blackColor],
MMtextColor: [UIColor whiteColor],
MMtoolbarColor: [UIColor darkGrayColor],
MMbuttonColor: [UIColor whiteColor],
MMfont: customFont,
MMValueY: @5};
[MMPickerView showPickerViewInView:self.view
withStrings:strings
withOptions:options
completion:^(NSString *selectedString) {
//selectedString is the return value which you can use as you wish
self.label.text = selectedString;
}];
一个有用的功能是让 PickerView 选择上一次选中的内容。这两种展示方法都可以轻松实现,如示例 4 中所示。
@interface ViewController ()
@property (nonatomic, strong) NSString * selectedString;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.selectedString = [_stringsArray objectAtIndex:0];
}
- (IBAction)showPickerViewButtonPressed:(id)sender {
NSArray *strings = @[@"This", @"is", @"just", @"an array", @"of strings."];
[MMPickerView showPickerViewInView:self.view
withStrings:strings
withOptions:@{selectedObject:_selectedString}
completion:^(NSString *selectedString) {
_label.text = selectedString;
_selectedString = selectedString;
}];
}
@end
MMPickerView 由 Madjid Mahdjoubi 提供。如果您有功能建议或错误报告,请通过发送 pull 请求或通过 创建新问题 来帮助。如果您正在项目中使用 MMPickerView,引用会很好。