我一直在寻找并尝试为此问题提供的所有代码示例,但我似乎无法让它正常工作。这是我第一次制作应用程序(除了完成教程之外),请原谅我要求一个愚蠢的'题。我只是一个新手。
我正在尝试创建一个iOS应用程序来注册启动和放大器结束时间,服务类型和客户。一旦用户点击UITextField,UIPicker就会弹出显示信息。 我能够为开始和结束时间创建UIDatePicker,并为服务和客户创建一个空的UIPickerView。
我遇到的问题是我试图将UIPickerView的客户名称和服务类型添加到每个客户端。但到目前为止还没有成功。我已尝试添加数组,但UIPickerView中没有显示任何内容。在故事板中,我为每个项目添加了4个标签和4个UITextField。
.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 ViewController() @end
@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.
答案 0 :(得分:6)
似乎您的代码中没有任何地方设置UIPickerView
的委托和数据源。没有它们,选择器视图不知道要显示什么以及如何通知您用户已选择了某些内容。
请查看此处: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
好的,这是有趣的部分:
这里我们声明我们将遵守UIPickerViewDelegate
和UIPickerViewDataSource
协议。协议基本上是一组方法头。当您声明符合协议时,您需要实现它们(至少需要这些协议)。
这很明显,我们告诉选择器我们将成为它的委托和数据源。如果我们之前没有声明对协议的一致性,编译器会在这里开始给我们错误。
将调用UIPickerViewDataSource
中的此方法以了解应显示的组件数量。例如,日期选择器具有典型的3个组件(日,月,年)。
此方法想知道每个组件中将包含多少元素。
此方法是您实际提供要显示给用户的文本的地方。您还可以实现此方法的其他两种变体之一,以提供属性文本或完全自定义视图。
当动画在新项目上停止时,会调用此方法。
此示例中不需要pickerView == self.pickerView
的检查。我添加了它们,以便您知道如何确定哪个选择器(如图4所示)返回所需的信息。理想情况下,这需要进一步的重构,因为混乱的代理/数据源不是最好的主意。