我正在使用 textFields inputView 属性来显示 UIPickerViews 而不是键盘。按照我的编程方式,我没有在 .H 文件中声明 PickerViews,也没有在界面构建器的视图中连接它们。当我实现选择器视图的属性时,我无法使它们中的每一个都具有不同的属性。我尝试使用“if”语句来告诉它根据使用的选择器具有不同的属性,如下所示....
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)agePickerView {
if ([pickerView isEqual: agePickerView]){
return 1;
}
if ([pickerView isEqual: weightPickerView]){
return 1;
}
}
但是,当我这样做时,它会说 weightPickerView 未声明。所以这是我实现pickerViews的代码。
@implementation MainViewController
//WEIGHT FIELD CODE
-(IBAction)weightFieldDidBeginEditing{
UIPickerView *weightPickerView = [[UIPickerView alloc] init];
weightField.inputView = weightPickerView;
weightPickerView.showsSelectionIndicator = YES;
weightPickerView.delegate = self;
weightPickerView.dataSource = self;
[weightPickerView release];
UIToolbar *weightToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
[weightToolBar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
weightField.inputAccessoryView = weightToolBar;
}
-(NSInteger)weightpickerView:(UIPickerView *)weightPickerView numberOfRowsInComponent: (NSInteger)component{
return [weightPickerArray count];
}
-(NSString *)weightpickerView:(UIPickerView*)weightPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [weightPickerArray objectAtIndex:row];
}
// AGE FIELD CODE
-(IBAction)ageFieldDidBeginEditing{
UIPickerView *agePickerView = [[UIPickerView alloc] init];
ageField.inputView = agePickerView;
agePickerView.showsSelectionIndicator = YES;
agePickerView.delegate = self;
agePickerView.dataSource = self;
[agePickerView release];
UIToolbar *ageToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
[ageToolbar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
ageField.inputAccessoryView = ageToolbar;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)agePickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)agePickerView numberOfRowsInComponent: (NSInteger)component {
return [agePickerArray count];
}
-(NSString *)pickerView:(UIPickerView*)agePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [agePickerArray objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)agePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
ageField.text = (NSString *) [agePickerArray objectAtIndex:row];
}
-(void)pickerDoneClicked:(id)sender{
[ageField resignFirstResponder];
[weightField resignFirstResponder];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
agePickerArray = [[NSMutableArray alloc] init];
for (int age=1; age<=100; age++) {
NSString *ageString = [NSString stringWithFormat:@"%d%",age];
[agePickerArray addObject:ageString];
weightPickerArray = [[NSMutableArray alloc] init];
for (int weight = 50; weight<=500; weight++) {
NSString *weightString = [NSString stringWithFormat:@"%d%",weight];
[weightPickerArray addObject:weightString];
}
}
有没有其他方法可以做我想做的事情,因为这种方法行不通......如果我只是尝试重做每个选择器视图的实现,我会收到“重新定义......”“numberOfRowsInComponent”的错误“”或我正在做的任何其他属性。请帮助,即使我必须更改它并以某种方式在 .H 文件中声明pickerViews,只要我能做到这一点,我不在乎我必须做什么工作!!
谢谢
更新!!!
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource> {
IBOutlet UITextField*ageField;
NSMutableArray *agePickerArray;
IBOutlet UITextField*weightField;
NSMutableArray *weightPickerArray;
UIPickerView *weightPickerView;
UIPickerView *agePickerView;
}
- (IBAction)ageFieldDidBeginEditing;
- (IBAction)weightFieldDidBeginEditing;
- (IBAction)showInfo:(id)sender;
- (IBAction)activityButtonClicked;
@end
.m 文件
-(IBAction)weightFieldDidBeginEditing{
//UIPickerView *weightPickerView = [[UIPickerView alloc] init];
weightField.inputView = weightPickerView;
weightPickerView.showsSelectionIndicator = YES;
weightPickerView.delegate = self;
weightPickerView.dataSource = self;
[weightPickerView release];
UIToolbar *weightToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
[weightToolBar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
weightField.inputAccessoryView = weightToolBar;
}
-(NSInteger)weightpickerView:(UIPickerView *)weightPickerView numberOfRowsInComponent:(NSInteger)component{
return [weightPickerArray count];
}
-(NSString *)weightpickerView:(UIPickerView*)weightPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [weightPickerArray objectAtIndex:row];
}
// 年龄字段代码
-(IBAction)ageFieldDidBeginEditing{
//UIPickerView *agePickerView = [[UIPickerView alloc] init];
ageField.inputView = agePickerView;
agePickerView.showsSelectionIndicator = YES;
agePickerView.delegate = self;
agePickerView.dataSource = self;
[agePickerView release];
UIToolbar *ageToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
[ageToolbar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
ageField.inputAccessoryView = ageToolbar;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)agePickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if ([pickerView isEqual: agePickerView]) {
return [agePickerArray count];
}
if ([pickerView isEqual:weightPickerView]) {
return [weightPickerArray count];
}
}
-(NSString *)pickerView:(UIPickerView*)agePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [agePickerArray objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)agePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
ageField.text = (NSString *) [agePickerArray objectAtIndex:row];
}
-(void)pickerDoneClicked:(id)sender{
[ageField resignFirstResponder];
[weightField resignFirstResponder];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
agePickerArray = [[NSMutableArray alloc] init];
for (int age=1; age<=100; age++) {
NSString *ageString = [NSString stringWithFormat:@"%d%",age];
[agePickerArray addObject:ageString];
weightPickerArray = [[NSMutableArray alloc] init];
for (int weight = 50; weight<=500; weight++) {
NSString *weightString = [NSString stringWithFormat:@"%d%",weight];
[weightPickerArray addObject:weightString];
}
}