該代碼的基本前提是它有兩個字段,一個文本字段用於存儲人的高度,另一個用英寸存儲高度。因此,當有人點擊英尺文本字段或英寸文本字段時,會彈出一個pickerview,允許用戶選擇高度。不過,我發現了以下錯誤:[__NSArrayI pickerView:numberOfRowsInComponent:]:無法識別的選擇器發送到實例
[__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不再具有委託。我不確定這是否是問題,如果是如何解決這個問題,任何人都可以幫忙嗎?
謝謝!
如何定義ftPicker屬性?你正在使用ARC還是MRC? – rmaddy
ARC,定義如下: @property(strong,nonatomic)NumberPickerView * ftPicker; – user1549792
更新您的問題,顯示如何使用'GetUserStatistics'類以及對'createNumberPickerViewWithStartingValue'的調用。也許'GetUserStatistics'實例被取消分配,這意味着'ftPicker'被釋放。 – rmaddy