gpt4 book ai didi

ios - [__NSArrayI pickerView :numberOfRowsInComponent:]: unrecognized selector sent to instance

转载 作者:行者123 更新时间:2023-11-28 21:34:20 25 4
gpt4 key购买 nike

这段代码的基本前提是它有两个字段,一个以英尺为单位存储人的高度的文本字段,一个以英寸为单位存储高度的文本字段。因此,当有人单击英尺文本字段或英寸文本字段时,会弹出一个选择器 View ,允许用户选择高度。但是,我收到以下错误:

[__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 协议(protocol)):

-(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 在函数返回 View 后被销毁,因此 pickerViewFeet 不再有委托(delegate)。我不确定这是否是问题所在,如果是解决方法,有人可以帮忙吗?

谢谢!

最佳答案

您调用 createNumberPickerViewWithStartingValue 两次。第二次调用导致 ftPicker 被重置为 NumberPickerView 的新实例。这意味着分配给第一个选择器 View 的第一个实例将被释放。这会导致崩溃。

您需要重新组织代码,这样就不会使用同一个实例变量来保存两个 NumberPickerView 实例。

您还需要更新的教程。在大多数情况下,您不应该调用 @synthesize。并且您对属性实例变量的所有引用都应更改为对实际属性的引用。

关于ios - [__NSArrayI pickerView :numberOfRowsInComponent:]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34523106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com