我正在尝试调出一个选择器视图而不是键盘来放入文本字段,但我的选择器视图没有显示。
#import "SettingsViewController.h"
#import "FindClasses.h"
@interface SettingsViewController ()
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
@property (weak, nonatomic) IBOutlet UITextField *classTextField;
@property (strong, nonatomic) UIPickerView *picker;
@property (nonatomic, strong) FindClasses *finder;
@property (nonatomic, strong) NSArray *itemsArray;
@end
@implementation SettingsViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *itemsArray = [self.finder findClassesInTimetable];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)presentPicker {
CGRect frame = CGRectMake(0, 40, 0, 0);
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:frame];
picker.delegate = self;
picker.dataSource = self;
// TODO: animate this on screen
[self.view addSubview:picker];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[self presentPicker];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.itemsArray count]; //where items is your array of items
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row inComponent:(NSInteger)component {
return [self.itemsArray objectAtIndex:row];
}
有人可以告诉我我做错了什么吗?我从另一个Stackoverflow帖子中获取了示例:
答案 0 :(得分:1)
希望它可以帮到你
- (void)presentPicker {
UIPickerView *picker = [[UIPickerView alloc] init];
picker.delegate = self;
picker.dataSource = self;
// TODO: animate this on screen
classTextField .delegate = self;
classTextField.inputView = picker;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[textField becomeFirstResponder];
return YES;
}
答案 1 :(得分:1)
这是我使用UIPickerView
的方式,示例代码如下:
UIPickerView* genderPicker = [[UIPickerView alloc] init];
genderPicker.delegate = self;
genderPicker.dataSource = self;
初始化UITextField
(在我的情况下为registrationGenderField):
registrationGenderField.inputView = genderPicker;
然后是delegate
和dataSource
s:
- (NSString *) pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
switch (row) {
case 0:
return @"Gender";
break;
case 1:
return @"Male";
break;
case 2:
return @"Female";
break;
default:
return @"Gender";
break;
}
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
switch (row) {
case 0:
registrationGenderField.text = @"";
break;
case 1:
registrationGenderField.text = @"Male";
break;
case 2:
registrationGenderField.text = @"Female";
break;
default:
registrationGenderField.text = @"";
break;
}
}
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 3;
}
答案 2 :(得分:0)
你只需要为你的pickerview分配init;
并将其委托设置为self,并在.h文件中添加uipickerviewdelegate和datasource
比textfieldbegin编辑添加[textField setInputView:picker];
还实现textfieldshouldreturn