2016-04-13 12 views
0
#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ファイルコードです。私がシミュレータを実行すると、Heightの最初のテキストフィールドが正常に動作していますが、WeightとDateテキストフィールドではピッカービューに例外があります。NSRangeException複数のテキストフィールドに複数のpickerViewがある場合

Simulator screen shot

NSRangeException screen shot

+0

と答えてください。私は2つの写真について間違えていました。私の体重リストはxcodeの.plistファイルにあります。 –

+0

事前に感謝、私はちょうどエントリレベルのiosの開発者は、この問題を解決する方法を助けてください。 –

答えて

0

まあ例外はIndex 5 beyond bounds [0..0]を言います。つまり、サイズ1の配列がありますが、インデックス5のオブジェクトを要求していますが、それは意味をなさないので、クラッシュします。

から呼び出されているselectedRowInComponent:からobjectAtIndexメソッドが呼び出されています。これは、無効なコンポーネント番号をselectedRowInComponentに渡していることを示しています。

weightComponentmonth/day/yearComponentが設定されている場所はわかりませんが、0と1、2とそれぞれ設定されていないと推測しています。

また、ラベル0ft1ftなどのピッカーを使用している場合は、これらのテーブルをすべて保持しないようにします。テーブルを作成してインデックスを作成するのではなく、return [NSString stringWithFormat:@"%dft",row];

+0

はい、あなたは正しいです。 .hファイルでは、コンポーネントを0と0、1、および2に設定する必要があります。ありがとう、問題解決! –

+0

もう1つの無料の提案!日付は厳しく、特に国々では難しいです。 AppleのUIDatePickerを使用するのに最適です(両方を追加できますが、一度に隠すことはできません)。 (また、FYI、StackOverflowで、誰かが答えを提供したら、左の列にあるチェックマークでそれを受け入れるべきです。) – mackworth

+0

ありがとう、私はすでにそれをしました。 –