gpt4 book ai didi

ios - 如何在点击 UITextField 时在 UIPickerView 中显示信息

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

我一直在寻找并尝试为这个问题提供的所有代码示例,但我似乎无法让它工作。这是我第一次制作应用程序(除了完成教程)所以请原谅我问了一个“愚蠢”的问题。我只是一个新手。

我正在尝试创建一个 iOS 应用程序来注册开始和结束时间、服务类型和客户。一旦用户点击 UITextField,UIPicker 就会弹出显示信息。我能够为开始时间和结束时间创建 UIDatePicker,并为服务和客户创建一个空的 UIPickerView。

我遇到的问题是我试图将每个客户的名称和服务类型添加到 UIPickerView。然而到目前为止还没有成功。我试过添加一个数组,但 UIPickerView 中没有显示任何内容。在 Storyboard 中,我为每个项目添加了 4 个标签和 4 个 UITextFields。

.h 的代码如下所示:

@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
UIDatePicker *datePicker;
UIDatePicker *datePicker2;
NSArray *dienstArray;
NSArray *klantArray;
}

@property (strong, nonatomic) IBOutlet NSArray *dienstArray; //Service Array
@property (strong, nonatomic) IBOutlet NSArray *klantArray; //Customer Array
@property (weak, nonatomic) IBOutlet UIPickerView *dienstPicker; //Service PickerView
@property (weak, nonatomic) IBOutlet UIPickerView *klantPicker; //Customer PickerView
@property (weak, nonatomic) IBOutlet UITextField *dienstTextField; //Service TextField
@property (weak, nonatomic) IBOutlet UITextField *klantTextField; //Customer TextField
@property (weak, nonatomic) IBOutlet UITextField *dateSelectionTextField; //BeginDate TextField
@property (weak, nonatomic) IBOutlet UITextField *dateSelectionTextField2; //EndDate TextField

@end

这是.m的代码 @interface View Controller () @结束

@implementation ViewController
@synthesize dienstPicker;
@synthesize klantPicker;
@synthesize klantArray;
@synthesize dienstArray;

- (void)viewDidLoad {
[super viewDidLoad];


//BeginDatumTijdPicker
datePicker=[[UIDatePicker alloc]init];
datePicker.datePickerMode=UIDatePickerModeDateAndTime;

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"NL"]; //Date notation 24hours
[datePicker setLocale: locale];

// Styling for the UIPickerView
[self.dateSelectionTextField setInputView:datePicker]; //shows the UIPickerView
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(ShowSelectedDate1)]; //When clicked "Done" the UIPickerView disappears.
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
[self.dateSelectionTextField setInputAccessoryView:toolBar];

//EindDatumTijdPicker
// [Same as BeginDatumTijdPicker but different names]

//DienstPicker

dienstPicker =[[UIPickerView alloc] init];
dienstArray = [[NSArray alloc] initWithObjects: @"test1", @"test2", @"test3", @".....", nil];
self.dienstPicker.delegate = self;
self.dienstPicker.dataSource = self;
self.dienstPicker.showsSelectionIndicator = YES;

// Styling for the UIPickerView
[self.dienstTextField setInputView:dienstPicker]; //shows the UIPickerView
UIToolbar *toolBar3 = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolBar3 setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn3=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(ShowSelectedDienst)]; //When clicked "Done" the UIPickerView disappears.
UIBarButtonItem *space3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar3 setItems:[NSArray arrayWithObjects:space3,doneBtn3,nil]];
[self.dienstTextField setInputAccessoryView:toolBar3];


//KlantPicker

klantPicker =[[UIPickerView alloc] init];
klantArray = [[NSArray alloc] initWithObjects: @"test1.1", @"test2.1", @"test3.1", @".....", nil];
self.klantPicker.delegate= self;
self.klantPicker.dataSource = self;
self.klantPicker.showsSelectionIndicator = YES;

// Styling for the UIPickerView
[self.klantTextField setInputView:klantPicker]; //shows the UIPickerView
UIToolbar *toolBar4 = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolBar4 setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn4 =[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(ShowSelectedKlant)]; //When clicked "Done" the UIPickerView disappears.
UIBarButtonItem *space4 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar4 setItems:[NSArray arrayWithObjects:space4,doneBtn4,nil]];
[self.klantTextField setInputAccessoryView:toolBar4];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

//ShowSelectedService (When clicking the "Done" button, the UIPickerView disappears. )

-(void)ShowSelectedDienst{
[self.dienstTextField resignFirstResponder];
}

//ShowSelectedClient (When clicking the "Done" button, the UIPickerView disappears. )

-(void)ShowSelectedKlant{
[self.klantTextField resignFirstResponder];

}

// The code below
// works just fine.
//DatePicker Begin tijd
-(void)ShowSelectedDate1 {
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"dd MMM HH:mm"]; //formats date & time
self.dateSelectionTextField.text=[NSString stringWithFormat:@"%@",[formatter stringFromDate:datePicker.date]];
[self.dateSelectionTextField resignFirstResponder];
}

//!!DatePicker Eind tijd!!
//Same as DatePicker Begin Tijd.
@end

我希望一切都清楚,并且有人可以帮助我。这是应用程序的样子:

When the Service textfield is clicked the UIPickerView pops up.

最佳答案

看起来,您在代码中的任何地方都没有设置 UIPickerView 的委托(delegate)和数据源。没有它们,选择器 View 就不知道要显示什么以及如何通知您用户已经选择了一些东西。

请看这里:UIPickerViewDataSource在这里 UIPickerViewDelegate如果有什么不清楚或您在实现这些方面遇到麻烦,请告诉我。


这是一个例子:

@interface ViewController () <UIPickerViewDelegate, UIPickerViewDataSource> //#1

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *pickerNames;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.pickerView = [[UIPickerView alloc] init];
self.pickerView.delegate = self; //#2
self.pickerView.dataSource = self; //#2

self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.frame), 50)];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.placeholder = @"What's your favourite programming language?";
self.textField.inputView = self.pickerView;

self.pickerNames = @[ @"Objective-C", @"Swift", @"Java", @"C", @"C++", @"Other"];

[self.view addSubview:self.textField];
}

#pragma mark - UIPickerViewDataSource

// #3
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (pickerView == self.pickerView) {
return 1;
}

return 0;
}

// #4
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView == self.pickerView) {
return [self.pickerNames count];
}

return 0;
}

#pragma mark - UIPickerViewDelegate

// #5
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (pickerView == self.pickerView) {
return self.pickerNames[row];
}

return nil;
}

// #6
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (pickerView == self.pickerView) {
self.textField.text = self.pickerNames[row];
}
}

@end

好的,这里是有趣的部分:

  1. 在这里我们声明我们将遵守UIPickerViewDelegateUIPickerViewDataSource 协议(protocol)。协议(protocol)本质上是一组方法 header 。当你声明你将遵守一个协议(protocol)时,你需要实现它们(至少是那些被要求的)。

  2. 这很明显,我们告诉选择器我们将成为它的委托(delegate)和数据源。如果我们之前没有声明符合协议(protocol),编译器就会开始在这里给我们错误。

    1. UIPickerViewDataSource 中的这个方法将被调用以了解应显示多少个组件。例如,日期选择器通常有 3 个组成部分(日、月、年)。

    2. 此方法想知道每个组件中将有多少个元素。

    3. 此方法是您实际提供要显示给用户的文本的地方。您还可以实现此方法的其他两种变体之一,以提供属性文本或完全自定义的 View 。

    4. 当动画在新项目上停止时调用此方法。

此示例中不需要检查 pickerView == self.pickerView。我已经添加了它们,这样您就知道如何决定让哪个选择器(看起来有 4 个)返回所需的信息。理想情况下,这需要进一步重构,因为让委托(delegate)/数据源困惑不是最好的主意。

关于ios - 如何在点击 UITextField 时在 UIPickerView 中显示信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33610322/

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