我有一个选择器视图,在点击按钮时出现。我需要根据先前选择器数据的选择更新选择器数据。我可能需要根据之前的选择器选择更新选择器3-4次。我试过但无法解决。我引用了 MMPickerView 库 按钮点击我得到选择器。但是对于选择器的选择没有任何反应。
这是我的代码:
#import <UIKit/UIKit.h>
extern NSString * const pBackgroundColor;
extern NSString * const pToolbarColor;
extern NSString * const pFont;
extern NSString * const pYValue;
extern NSString * const pSelectedObject;
extern NSString * const pShowsSelectionIndicator;
extern NSString * const pTextAlignment;
@class PickerView;
@protocol PickerViewDelegate <NSObject>
-(void)didSelectTitle:(NSString*)title withPickerView:(PickerView >*)pickerView;
@end
@interface PickerView : UIView<UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) id<PickerViewDelegate> pickerDelegate;
@property (strong, nonatomic) NSArray *pickerDataArray;
@property (strong, nonatomic) UIPickerView *pickerView;
@property (nonatomic, assign) BOOL pickerViewShowsSelectionIndicator;
+(void)showPickerViewInView: (UIView *)view
withStrings: (NSArray *)strings
withOptions: (NSDictionary *)options
completion: (void(^)(NSString *selectedString))completion;
+(void)dismissWithCompletion: (void(^)(NSString *))completion;
-(void)reloadPickerView;
@end
#import "PickerView.h"
NSString * const pBackgroundColor = @"backgroundColor";
NSString * const pToolbarColor = @"toolbarColor";
NSString * const pFont = @"font";
NSString * const pYValue = @"yValueFromTop";
NSString * const pSelectedObject = @"selectedObject";
NSString * const pShowsSelectionIndicator = @"showsSelectionIndicator";
NSString * const pTextAlignment = @"textAlignment";
@interface PickerView()
@property (nonatomic, assign) CGFloat yValueFromTop;
@property (nonatomic, strong) UIView *pickerContainerView;
@property (nonatomic, strong) UIView *pickerViewContainerView;
@property (nonatomic, strong) UIView *pickerTopBarView;
@property (nonatomic, strong) UIToolbar *pickerViewToolBar;
@property (nonatomic, strong) UIBarButtonItem *pickerViewDoneBarButtonItem;
@property (nonatomic, strong) UIBarButtonItem *pickerViewCancelBarButtonItem;
@property (copy) void (^onDismissCompletion)(NSString *);
@property (copy) NSString *(^objectToStringConverter)(id object);
@property (nonatomic, assign) NSInteger pickerViewTextAlignment;
@end
@implementation PickerView
#pragma mark - singleton
+(PickerView *)sharedView {
static dispatch_once_t once;
static PickerView *sharedView;
dispatch_once (&once, ^{sharedView = [[self alloc] init];});
return sharedView;
}
+(void)showPickerViewInView:(UIView *)view
withStrings:(NSArray *)strings
withOptions:(NSDictionary *)options
completion:(void (^)(NSString *))completion{
[[self sharedView] initializePickerViewInView:view
withArray:strings
withOptions:options];
[[self sharedView] setPickerHidden:NO callBack:nil];
[self sharedView].onDismissCompletion = completion;
[view addSubview:[self sharedView]];
}
-(void)initializePickerViewInView: (UIView *)view
withArray: (NSArray *)array
withOptions: (NSDictionary *)options {
_pickerDataArray = array;
id chosenObject = options[pSelectedObject];
NSInteger selectedRow = 0;
if (chosenObject!=nil) {
selectedRow = [_pickerDataArray indexOfObject:chosenObject];
}else{
selectedRow = [[_pickerDataArray objectAtIndex:0] integerValue];
}
NSNumber *textAlignment = [[NSNumber alloc] init];
textAlignment = options[pTextAlignment];
//Default value is NSTextAlignmentCenter
_pickerViewTextAlignment = 1;
if (textAlignment != nil) {
_pickerViewTextAlignment = [options[pTextAlignment] integerValue];
}
BOOL showSelectionIndicator = [options[pShowsSelectionIndicator] boolValue];
if (!showSelectionIndicator) {
_pickerViewShowsSelectionIndicator = 1;
}
_pickerViewShowsSelectionIndicator = showSelectionIndicator;
UIColor *pickerViewBackgroundColor = [[UIColor alloc] initWithCGColor:[options[pBackgroundColor] CGColor]];
UIColor *toolbarBackgroundColor = [[UIColor alloc] initWithCGColor:[options[pToolbarColor] CGColor]];
UIFont *pickerViewFont = [[UIFont alloc] init];
pickerViewFont = options[pFont];
_yValueFromTop = [options[pYValue] floatValue];
[self setFrame: view.bounds];
[self setBackgroundColor:[UIColor clearColor]];
UIImage *toolbarImage = options[toolbarBackgroundColor];
if (toolbarImage!=nil) {
[_pickerViewToolBar setBackgroundImage:toolbarImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
}
//Whole screen with PickerView and a dimmed background
_pickerViewContainerView = [[UIView alloc] initWithFrame:view.bounds];
[_pickerViewContainerView setBackgroundColor: [UIColor colorWithRed:0.412 green:0.412 blue:0.412 alpha:0.7]];
[self addSubview:_pickerViewContainerView];
//PickerView Container with top bar
_pickerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, _pickerViewContainerView.bounds.size.height - 260.0, 320.0, 260.0)];
//Default Color Values (if colors == nil)
if (pickerViewBackgroundColor==nil) {
pickerViewBackgroundColor = [UIColor whiteColor];
}
_pickerContainerView.backgroundColor = pickerViewBackgroundColor;
[_pickerViewContainerView addSubview:_pickerContainerView];
//Content of pickerContainerView
//Top bar view
_pickerTopBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, _pickerContainerView.frame.size.width, 44.0)];
[_pickerContainerView addSubview:_pickerTopBarView];
[_pickerTopBarView setBackgroundColor:[UIColor whiteColor]];
_pickerViewToolBar = [[UIToolbar alloc] initWithFrame:_pickerTopBarView.frame];
[_pickerContainerView addSubview:_pickerViewToolBar];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
_pickerViewDoneBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismiss)];
// _pickerViewCancelBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonClicked:)];
_pickerViewToolBar.items = @[flexibleSpace, _pickerViewDoneBarButtonItem];
//Add pickerView
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 320.0, 216.0)];
_pickerView.frame = CGRectMake(0.0, 44.0, 320.0, 216.0);
[_pickerView setDelegate:self];
[_pickerView setDataSource:self];
[_pickerView setShowsSelectionIndicator: _pickerViewShowsSelectionIndicator];//YES];
[_pickerContainerView addSubview:_pickerView];
[_pickerContainerView setTransform:CGAffineTransformMakeTranslation(0.0, CGRectGetHeight(_pickerContainerView.frame))];
//Set selected row
[_pickerView selectRow:selectedRow inComponent:0 animated:YES];
}
#pragma mark - Show/hide PickerView methods
-(void)setPickerHidden: (BOOL)hidden
callBack: (void(^)(NSString *))callBack {
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
if (hidden) {
[_pickerViewContainerView setAlpha:0.0];
[_pickerContainerView setTransform:CGAffineTransformMakeTranslation(0.0, CGRectGetHeight(_pickerContainerView.frame))];
} else {
[_pickerViewContainerView setAlpha:1.0];
[_pickerContainerView setTransform:CGAffineTransformIdentity];
}
} completion:^(BOOL completed) {
if(completed && hidden){
[PickerView removePickerView];
callBack([self selectedObject]);
}
}];
}
-(void)pickerVwHidden: (BOOL)hidden{
if (hidden) {
[_pickerViewContainerView setAlpha:0.0];
[_pickerContainerView setTransform:CGAffineTransformMakeTranslation(0.0, CGRectGetHeight(_pickerContainerView.frame))];
} else {
[_pickerViewContainerView setAlpha:1.0];
[_pickerContainerView setTransform:CGAffineTransformIdentity];
}
}
#pragma mark - Dismiss Methods
+(void)dismissWithCompletion:(void (^)(NSString *))completion{
[[self sharedView] setPickerHidden:YES callBack:completion];
}
-(void)dismiss{
[PickerView dismissWithCompletion:self.onDismissCompletion];
}
+(void)removePickerView{
[[self sharedView] removeFromSuperview];
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
return [_pickerDataArray count];
}
- (NSString *)pickerView: (UIPickerView *)pickerView
titleForRow: (NSInteger)row
forComponent: (NSInteger)component {
if (self.objectToStringConverter == nil){
return [_pickerDataArray objectAtIndex:row];
} else{
return (self.objectToStringConverter ([_pickerDataArray objectAtIndex:row]));
}
}
#pragma mark - UIPickerViewDelegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *title = [_pickerDataArray objectAtIndex:row];
if ([_pickerDelegate respondsToSelector:@selector(didSelectTitle:withPickerView:)]) {
if (self.objectToStringConverter == nil) {
self.onDismissCompletion ([_pickerDataArray objectAtIndex:row]);
[self.pickerDelegate didSelectTitle:title withPickerView:self];
}
else{
NSString *title = [_pickerDataArray objectAtIndex:row];
self.onDismissCompletion (self.objectToStringConverter ([self selectedObject]));
[self.pickerDelegate didSelectTitle:title withPickerView:self];
}
}
}
- (id)selectedObject {
return [_pickerDataArray objectAtIndex: [self.pickerView selectedRowInComponent:0]];
}
-(void)reloadPickerView
{
[_pickerView reloadAllComponents];
}
@end
#import <UIKit/UIKit.h>
#import "PickerView.h"
@interface ViewController : UIViewController <PickerViewDelegate>
@property (strong, nonatomic) IBOutlet UIButton *sellerTypeButton;
@property (nonatomic, strong) NSArray *stringsArray;
@property (nonatomic, strong) NSString * selectedString;
@end
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) PickerView *pickrView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)sellerTypeButtonClicked:(id)sender {
_stringsArray = @[@"China",@"India",@"Sri Lanka"];
_selectedString = [_stringsArray objectAtIndex:0];
[PickerView showPickerViewInView:self.view
withStrings:_stringsArray
withOptions:@{pBackgroundColor:[UIColor whiteColor],
pSelectedObject:_selectedString,
pToolbarColor:[UIColor whiteColor],
pFont:[UIFont systemFontOfSize:18],
pYValue:@3} completion:^(NSString *selectedString){
[_sellerTypeButton setTitle:selectedString forState:UIControlStateNormal];
_selectedString = selectedString;
}];
}
-(void)didSelectTitle:(NSString*)title withPickerView:(PickerView *)pickerView
{
if ([_pickrView.pickerDataArray objectAtIndex:1]) {
_pickrView.pickerDataArray = [NSMutableArray arrayWithObjects:@"Mumbai",@"Kanpur",@"Bangalore", nil];
}
[_pickrView reloadPickerView];
}
@end
答案 0 :(得分:0)
在PickerView.h中简单地声明+(PickerView *)sharedView。然后只需在从sharedView获取实例后再更新pickerDataArray,如
PickerView *_pickerView = [PickerView sharedView];
_pickerView.pickerDataArray = [@"a",@"b",@"c"];
//then reload the picker after updating array
[_pickerView.pickerView reloadAllComponents];