簡體   English   中英

當我有多個文本字段的多個pickerViews時,出現NSRangeException

[英]NSRangeException when I have multiple pickerViews for multiple textfields

#import "ProfileViewController.h"

@implementation ProfileViewController{
    NSArray *currentArray;
    UITextField *currentTextField;
}

@synthesize picker, Feets, Inchs, Weights, Months, Days, Years, HeightValue, WeightValue, DOBValue;
@synthesize scrollView;
int variabla;

// Control the textfield go up when tap the textfield and keyboard coming out
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGPoint scrollPoint = CGPointMake(0, textField.frame.origin.y);
    [scrollView setContentOffset:scrollPoint animated:YES];
    [textField resignFirstResponder];
    [picker setHidden:YES];
    currentTextField = textField;
    NSLog(@"222222");
    if (currentTextField == HeightValue)
    {
        NSLog(@"3333333");
        [HeightValue resignFirstResponder];
        [picker setHidden:NO];
        variabla = 1;
    }
    else if (currentTextField == WeightValue)
    {
        NSLog(@"4444444");
        [WeightValue resignFirstResponder];
        [picker setHidden:NO];
        variabla = 2;
    }
    else if (currentTextField == DOBValue){
        NSLog(@"5555555");
        [DOBValue resignFirstResponder];
        [picker setHidden:NO];
        variabla = 3;
    }
    NSLog(@"variabla %d",variabla);
    [picker reloadAllComponents];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"111111");
 NSString *path = [[NSBundle mainBundle]pathForResource:@"WeightList" ofType:@"plist"];
    Weights = [[NSMutableArray alloc]initWithContentsOfFile:path];

    [picker setHidden:YES];

 Feets = [[NSMutableArray alloc]initWithObjects:@"0ft", @"1ft", @"2ft", @"3ft", @"4ft", @"5ft", @"6ft", @"7ft", @"8ft", @"9ft",nil];
    Inchs = [[NSMutableArray alloc]initWithObjects:@"0in", @"1in", @"2in", @"3in", @"4in", @"5in", @"6in", @"7in", @"8in", @"9in", @"10in", @"11in",nil];
    Months = [[NSMutableArray alloc]initWithObjects:@"1", @"2", @"3",nil];
    Days = [[NSMutableArray alloc]initWithObjects:@"1", @"2", @"3",nil];
    Years = [[NSMutableArray alloc]initWithObjects:@"1", @"2", @"3",nil];


#pragma mark - UIPcikerView DataSource and Delegate method
    // returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if (variabla == 1){
        return 2;
    }
    else if (variabla == 2){
        return 1;
    }
    else
        return 3;
}

    // returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (variabla == 1){
        if (component == feetComponent)
            return [Feets count];
        if (component == inchComponent)
            return [Inchs count];
    }
    if (variabla == 2){
        return [Weights count];
    }
    else{
        if (component == monthComponent)
            return [Months count];
        if (component == dayComponent)
            return [Days count];
        else
            return [Years count];
    }
}


  // set the row text to the textfield
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED{
    if (variabla == 1){
        if (component == feetComponent)
            return Feets[row];
        if (component == inchComponent)
            return Inchs[row];
    }
    if (variabla == 2){
        return Weights[row];
    }
    else{
        if (component == monthComponent)
            return Months[row];
        if (component == dayComponent)
            return Days[row];
        else
            return Years[row];
    }
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED{
    if (currentTextField == HeightValue){
        NSInteger feetRow = [picker selectedRowInComponent:feetComponent];
        NSInteger inchRow = [picker selectedRowInComponent:inchComponent];
        NSString *feet = Feets[feetRow];
        NSString *inch = Inchs[inchRow];
        NSString *msg = [[NSString alloc]initWithFormat:@"%@ %@", feet, inch];
        HeightValue.text = msg;
    }
    if (currentTextField == WeightValue){
        NSInteger weightRow = [picker selectedRowInComponent:weightComponent];
        NSString *weight = Weights[weightRow];
        NSString *msg2 = [[NSString alloc]initWithFormat:@"%@",weight];
        WeightValue.text = msg2;
    }
    if (currentTextField == DOBValue){
        NSInteger monthRow = [picker selectedRowInComponent:monthComponent];
        NSInteger dayRow = [picker selectedRowInComponent:dayComponent];
        NSInteger yearRow = [picker selectedRowInComponent:yearComponent];
        NSString *month = Months[monthRow];
        NSString *day = Days[dayRow];
        NSString *year = Years[yearRow];
        NSString *msg3 = [[NSString alloc]initWithFormat:@"%@ / %@ / %@", month, day, year];
        WeightValue.text = msg3;
    }

}

@end

上面是.m文件代碼。 當我運行模擬器時,“高度”的第一個文本字段工作正常,但是在“權重和日期”文本字段中,選擇器視圖出現了異常。

模擬器屏幕截圖

NSRangeException屏幕截圖

好吧,異常表明Index 5 beyond bounds [0..0] 就是說,您有一個大小為1的數組,但正在向索引5的對象請求它,這沒有意義,因此會崩潰。

追溯說,是從selectedRowInComponent:中調用objectAtIndex方法,這是從didSelectRow:inComponent 因此,這表明您要將無效的組件號傳遞給selectedRowInComponent

我不知道在哪里設置weightComponentmonth/day/yearComponent ,但是我猜測它們沒有分別設置為0和0、1和2。

0ft 1ft ,我還要補充一點,當您有一個帶有0ft1ft等標簽的選擇器時,您並不需要保留所有這些表。 無需創建表並為其titleForRow索引,只需使用return [NSString stringWithFormat:@"%dft",row];回答titleForRow調用return [NSString stringWithFormat:@"%dft",row];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM