gpt4 book ai didi

ios - PickerView 没有显示

转载 作者:行者123 更新时间:2023-11-28 22:00:28 25 4
gpt4 key购买 nike

我试图调出一个选择器 View 而不是键盘来放入文本字​​段,但我的选择器 View 没有显示。

#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 帖子中拿了这个例子:

最佳答案

这就是使用UIPickerView的方式,带有示例代码:

UIPickerView* genderPicker = [[UIPickerView alloc] init];

genderPicker.delegate = self;

genderPicker.dataSource = self;

在初始化您的 UITextField 之后(在我的例子中是 registrationGenderField):

registrationGenderField.inputView = genderPicker;

然后是 delegatedataSource:

- (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;
}

关于ios - PickerView 没有显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25285968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com