Создать новый файл. Я назвал мой AEMPicker. .H:
@protocol AEMPickerDelegate <NSObject>
-(void)touchedPicker:(NSString *)string;
@optional
-(void)setInitialPickerValueToRow:(int)i inComponent:(int)j animated:(BOOL)k;
@end
@interface AEMPicker : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
UIPickerView *pickerView;
}
@property (nonatomic, strong) NSArray *contentArray;
@property (nonatomic, assign) id<AEMPickerDelegate> delegatePicker;
- (id)initWithArray:(NSArray *)contents inFrame:(CGRect)pickerFrame;
@end
.m:
#import "AEMPicker.h"
@implementation AEMPicker
@synthesize contentArray;
@synthesize delegatePicker;
- (id)initWithArray:(NSArray *)contents inFrame:(CGRect)pickerFrame
{
self = [super init];
if (self) {
contentArray = [NSArray arrayWithArray: contents];
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.delegate = self;
[self.view addSubview:pickerView];
}
return self;
}
-(void)setInitialPickerValueToRow:(int)i inComponent:(int)j animated:(BOOL)k{
[pickerView selectRow:i inComponent:j animated:k];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [contentArray count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [contentArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[self.delegatePicker touchedPicker:[contentArray objectAtIndex:row]];
}
@end
Теперь в классе, который вы хотите представить свой pickerView, после #import AEMPicker;
добавьте его в .h:
@interface YourClass : UIViewController <AEMPickerDelegate, UITextFieldDelegate>{
AEMPicker *picker;
UIPopoverController *pickerPopOver;
UIPopoverController *pOC;
CGRect popRect;
}
добавить в свой .m:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
popRect = CGRectMake(406, 110, 0, 0);
CGRect pickerRect = CGRectMake(0, 10, 0, 0);
NSArray *contents = [[NSArray alloc] initWithObjects:@"Object 1", @"Object 2", @"Object 3", nil];
picker = [[AEMPicker alloc] initWithArray:contents inFrame:pickerRect];
picker.delegatePicker = self;
pickerPopOver = [[UIPopoverController alloc] initWithContentViewController:picker];
pickerPopOver.popoverContentSize = CGSizeMake(320, 250);
[pickerPopOver presentPopoverFromRect:popRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:TRUE];
pOC = pickerPopOver;
}
-(void)touchedPicker:(NSString *)string{
[yourTextField setText:string];
[pickerPopOver dismissPopoverAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
[pOC dismissPopoverAnimated:NO];
return YES;
}
Это должно дать вам довольно простой поповер для выбора.