为您的 iOS 应用提供了一个易于使用和可定制的 PickerView。具有动画、设计选项、选择值和之前选择的值功能。它与 iOS 6 一起工作,甚至在 iOS 7 中表现更好,因为它让您可以更改 PickerView 的背景颜色。
重要提示:如果你的项目没有使用 ARC:你必须在你的目标设置 > 编译阶段 > 编译源文件中添加 -fobjc-arc
编译器标志到 MMPickerView.m
。
MMPickerView/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
/*
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)
*/
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 request 或通过 创建新问题 来帮助改进。如果您在项目中使用了 MMPickerView,注明出处将是一种美好的行为。