2015-12-30 72 views
0

該代碼的基本前提是它有兩個字段,一個文本字段用於存儲人的高度,另一個用英寸存儲高度。因此,當有人點擊英尺文本字段或英寸文本字段時,會彈出一個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不再具有委託。我不確定這是否是問題,如果是如何解決這個問題,任何人都可以幫忙嗎?

謝謝!

+0

如何定義ftPicker屬性?你正在使用ARC還是MRC? – rmaddy

+0

ARC,定義如下: @property(strong,nonatomic)NumberPickerView * ftPicker; – user1549792

+0

更新您的問題,顯示如何使用'GetUserStatistics'類以及對'createNumberPickerViewWithStartingValue'的調用。也許'GetUserStatistics'實例被取消分配,這意味着'ftPicker'被釋放。 – rmaddy

回答

2

你叫createNumberPickerViewWithStartingValue兩次。第二次通話導致ftPicker被重置爲NumberPickerView的新實例。這意味着分配給第一個選擇器視圖的第一個實例被取消分配。這導致崩潰。

您需要重新組織代碼,以便不使用相同的實例變量來保存兩個NumberPickerView實例。

你還需要一個更新的教程。在大多數情況下,您不應該致電@synthesize。並且所有對屬性實例變量的引用都應改爲對實際屬性的引用。

+0

謝謝!這是問題。 – user1549792

+0

但是,只是更多的問題,因爲我現在改變了代碼,所以不是ftPicker = [[NumberPickerView alloc] initWithStartingValue:startVal endingVal:endingVal];我有: NumberPickerView * tempNumPicker = [[NumberPickerView alloc] initWithStartingValue:startVal endingVal:endingVal]; 但是,這崩潰了,我相信這是因爲tempNumPicker是在本地聲明的,它在函數返回後被釋放。我怎樣才能解決這個問題? – user1549792

+1

你需要一個強大的參考通過使用伊娃。但是你需要爲每個實例分配一個ivar。 – rmaddy

0

第一次使用self.ftPicker代替ftpicker和:

-(NumberPickerView *) initWithStartingValue: (int) startingVal endingVal:(int) endingVal { 
    self = [super init]; 
    startingValue = startingVal; 
    endingValue = endingVal; 
    return self; 
} 
+0

我的numberpickerview被定義爲:@interface NumberPickerView:NSObject ,因此我不能這樣做[[super alloc] init]; – user1549792

+0

@ user1549792是的,你可以,你必須。雖然它應該是'self = [super init];'。 – rmaddy

+0

我的道歉,我不能做的分配,但我可以做自我= [超級初始化],但是,之後,它仍然沒有工作。 – user1549792

相關問題