我在 .h 文件中有这段代码:
#import <UIKit/UIKit.h>
@interface NFModalPickerView : NSObject
@end
@protocol NFModalPickerViewDelegate<NSObject>
@optional
- (void)titleSelected:(NFModalPickerView *) modalPickerView title:(NSString *) title;
@required
- (void)done:(NFModalPickerView *) modalPickerView;
@end
@interface NFModalPickerView()
{
id <NFModalPickerViewDelegate> delegate;
}
@property (nonatomic, strong) NSMutableArray * objectArray;
@property (nonatomic, strong) id <NFModalPickerViewDelegate> delegate;
- (void) show;
@end
和 .m 文件中的这段代码:
#import "NFModalPickerView.h"
@interface NFModalPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@end
@implementation NFModalPickerView
@synthesize objectArray;
@synthesize delegate;
UIActionSheet *actionSheet;
UIPickerView *pickerView ;
UISegmentedControl *closeButton;
- (void) show{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
pickerView = nil;
closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
closeButton = nil;
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [objectArray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [objectArray objectAtIndex:row];
}
-(void) dismissActionSheet:(id)sender {
UIActionSheet *actionSheet = (UIActionSheet *)[(UIView *)sender superview];
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
[[self delegate] titleSelected:self title:[objectArray objectAtIndex:row]];
}
@end
最后是我的 ViewController 中的这段代码
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
pickerView = [[NFModalPickerView alloc] init];
pickerView.objectArray = [[NSMutableArray alloc] init];
[pickerView.objectArray addObject:@"Don personnel"];
[pickerView.objectArray addObject:@"Don d'entreprise"];
[pickerView setDelegate:self];
[pickerView show];
return NO;
}
使用 NFModalPickerView 作为选取器视图的代表时,我总是遇到错误的访问错误。如果我将所有代码放在视图控制器中,使用视图控制器作为选取器视图的代表,它工作正常。我需要有一个单独的类来重用 modalpickerview,而不是总是将所有代码放在我的每个视图控制器中。任何人都可以帮助我吗?