我正在使用textFields inputView属性显示UIPickerViews而不是键盘。按照我编程的方式,我没有在.H文件中声明PickerViews,也没有在接口构建器的视图中连接它们。当我实现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];
}
}
最佳答案
UIPickerView *weightPickerView = [[UIPickerView alloc] init];
您需要在.h文件(或接口)中声明。
您是否要继承
UIPickerView
?编辑*如果由于某种原因不想在.h文件中声明它们,请为选择器设置一个
tag
。您所有的
UIPickerView
都将调用UIPickerViewDelegate方法,这意味着除非您继承并创建了自己的方法(我相信您不是),否则-(NSInteger)weightpickerView:(UIPickerView *)weightPickerView numberOfRowsInComponent: (NSInteger)component
不会被调用。-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
if([pickerView isEqual: agePickerView]){
return [agePickerArray count];
}else if([pickerView isEqual: weightPickerView]){
return [weightPickerArray count];
}else return 0;
}
如果您想使用标签来做;
weightPickerView.tag = 100; //set the tag
if(pickerView.tag == 100) //check which picker it is