这段代码的基本前提是它有两个字段,一个以英尺为单位存储人物身高的文本字段,另一个以英寸为单位存储高度的字段。因此,当有人点击脚文本字段或英寸文本字段时,弹出一个允许用户选择高度的选择器视图。但是,我收到以下错误:
[__NSArrayI pickerView:numberOfRowsInComponent:]: unrecognized selector sent to instance
当我运行以下代码时:
#import "GetUserStatistics.h"
@interface GetUserStatistics ()
@end
@implementation GetUserStatistics
@synthesize feetField, inchesField, pickerViewFeet, pickerViewInches, ftPicker, inPicker;
- (void)viewDidLoad {
[super viewDidLoad];
pickerViewFeet = [self createNumberPickerViewWithStartingValue:1 endingValue:8 defaultValue:5];
NSLog(@"%f", pickerViewFeet.frame.size.height);
feetField.inputView = pickerViewFeet;
NSLog(@"%f", feetField.inputView.frame.size.height);
//feetField.inputAccessoryView = [self createToolbar];
pickerViewInches = [self createNumberPickerViewWithStartingValue:0 endingValue:11 defaultValue:8];
NSLog(@"%f",pickerViewInches.frame.size.height);
inchesField.inputView = pickerViewInches;
NSLog(@"%f", inchesField.inputView.frame.size.height);
//NSLog(@"%@", pickerViewFeet.delegate);
//inchesField.inputAccessoryView = [self createToolbar];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}
-(void) inputAccessoryViewDidFinish{
feetField.text = [[NSString alloc]initWithFormat:@"%i", [(UIPickerView *)feetField.inputView selectedRowInComponent:0]];
inchesField.text = [[NSString alloc]initWithFormat:@"%i", [(UIPickerView *)inchesField.inputView selectedRowInComponent:0]];
actualHeight = ([feetField.text intValue])*12 + [inchesField.text intValue];
NSLog(@"actual height:%i", actualHeight);
[feetField endEditing:YES];
[inchesField endEditing:YES];
}
-(UIPickerView *) createNumberPickerViewWithStartingValue: (int) startVal endingValue: (int) endingVal defaultValue: (int) defaultValue{
UIPickerView * tempPicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 50, 100, 150)];
ftPicker = [[NumberPickerView alloc]initWithStartingValue:startVal endingVal:endingVal];
tempPicker.delegate = ftPicker;
[tempPicker selectRow:defaultValue inComponent:0 animated:NO];
return tempPicker;
}
-(UIToolbar * ) createToolbar{
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:
CGRectMake(0,0, 320, 44)]; //should code with variables to support view resizing
UIBarButtonItem *doneButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(inputAccessoryViewDidFinish)];
//using default text field delegate method here, here you could call
//myTextField.resignFirstResponder to dismiss the views
[myToolbar setItems:[NSArray arrayWithObject: doneButton] animated:NO];
return myToolbar;
}
-(void) addPickerViewToTextField: (UITextField **) textField pickerViewToAdd : (UIPickerView **) pickerView{
NSLog(@"dading view");
*pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 100, 150)];
}
@end
NumberPickerView代码(实现UIPickerViewDelegate协议):
-(NumberPickerView *) initWithStartingValue: (int) startingVal endingVal: (int) endingVal
{
startingValue = startingVal;
endingValue = endingVal;
return self;
}
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
NSLog(@"calling this function");
return endingValue - startingValue + 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [[NSString alloc]initWithFormat:@"%i", (int)(row) + startingValue];
}
我怀疑问题是UIPickerView的返回,特别是在函数返回视图后销毁了NumberPickerView,因此pickerViewFeet不再有委托。我不确定这是不是问题,如果是如何解决这个问题,任何人都可以帮忙吗?
谢谢!
答案 0 :(得分:2)
您拨打createNumberPickerViewWithStartingValue
两次。第二次调用会导致ftPicker
重置为NumberPickerView
的新实例。这意味着分配给第一个选择器视图的第一个实例将被释放。这导致了崩溃。
您需要重新组织代码,因此不会使用相同的实例变量来保存两个NumberPickerView
实例。
您还需要一个较新的教程。在大多数情况下,您不应该致电@synthesize
。并且所有对属性实例变量的引用都应该更改为对实际属性的引用。
答案 1 :(得分:0)
首先使用self.ftPicker
代替ftpicker
和:
-(NumberPickerView *) initWithStartingValue: (int) startingVal endingVal:(int) endingVal {
self = [super init];
startingValue = startingVal;
endingValue = endingVal;
return self;
}