2012-03-03 4 views
1

私はUIPickerViewに応答データを表示する以下のコードを実装しました。私はアプリケーションを実行するとクラッシュしました。どのように問題を解決するには?pickerviewのコードはどのように解析されたURLレスポンスをpickerviewに表示していますか?

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    self.view.frame=CGRectMake(0, 0, 320, 480); 
    materialarray = [[NSMutableArray alloc] init]; 



    [picbutton setTitle:[materialarray objectAtIndex:0] forState:UIControlStateNormal]; 
    [self testing]; 
     } 

-(IBAction)picbuttonaction:(id)sender{ 

    NSLog(@"unitpiker"); 
    [enterVal1 resignFirstResponder]; 
    [enterVal2 resignFirstResponder]; 


    str = picbutton.currentTitle; 
    UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:nil 
                 delegate:self 
              cancelButtonTitle:@"Done" 
             destructiveButtonTitle:nil 
              otherButtonTitles:nil]; 


    Picker = [[UIPickerView alloc] init]; 
    Picker.tag = 1; 
    Picker.showsSelectionIndicator = YES; 
    Picker.dataSource = self; 
    Picker.delegate = self; 

    [Picker selectRow:j inComponent:0 animated:YES]; 
    [menu addSubview:Picker]; 
    [menu showInView:self.view]; 
    [menu sendSubviewToBack:Picker];  
    [menu setBounds:CGRectMake(0,0,320, 300)]; 

    CGRect pickerRect = Picker.bounds; 
    Picker.bounds = pickerRect; 

    [Picker release]; 
    [menu release]; 
} 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; 
{ 
    if (pickerView.tag==1) { 
     return 1; 
    } 
    if(pickerView.tag==2){ 
     return 1; 
    } 
    return 0; 
} 
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    if (pickerView.tag==1){ 

     return [materialarray count]; 
    } 
    if(pickerView.tag==2){ 
     return [array_preferences count]; 
    } 
    return 0; 
} 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 
{ 
    if (pickerView.tag==1) { 

     return [materialarray objectAtIndex:row]; 
    } 
    if(pickerView.tag==2){ 
     return [array_preferences objectAtIndex:row]; 
    } 
    return 0; 
} 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; 
{ 
    j=row; 

    [picbutton setTitle:[materialarray objectAtIndex:row] forState:UIControlStateNormal]; 
    }  

-(IBAction)Resetbuttonaction{ 

    [picbutton setTitle:[materialarray objectAtIndex:0] forState:UIControlStateNormal]; 
    j=0; 
} 


-(void)testing{ 
    NSString *jobSearchUrlString = [NSString stringWithFormat:@"http://infra2appsmobile.cloudapp.net/Infra2Apps.svc/Contacts"]; 
    //NSLog(@"url for new articles is = %@",jobSearchUrlString); 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:jobSearchUrlString]]; 

    NSURLConnection *theconnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    if (theconnection) { 
     RoutData = [[NSMutableData alloc] init]; 
    } 

} 
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 

    [RoutData setLength: 0]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [RoutData appendData:data]; 

    NSString *thexml = [[NSString alloc] initWithData:RoutData encoding:NSUTF8StringEncoding]; 

    NSDictionary *dictXML= [XMLReader dictionaryForXMLString:thexml error:nil]; 

    NSMutableArray *arrStation = [[dictXML objectForKey:@"feed"] objectForKey:@"entry"] ;//this would 



    for (int i=0; i<[arrStation count]; i++){ 
     str=[[[[[arrStation objectAtIndex:i] objectForKey:@"content"] objectForKey:@"m:properties"] objectForKey:@"d:ContactName"] objectForKey:@"text"]; 
     // NSlog(@"name is : %@",str); 

     [materialarray addObject:str]; 
     [Picker reloadAllComponents]; 
     NSLog(@"name is->>>>>>> : %@",materialarray);   
    } 

    /


} 

私はそれを示してアプリケーションを実行すると:

2012-03-03 12:09:55.665 AdditionNumbers[1698:f803] Could not load the "dropdown2.png" image referenced from a nib in the bundle with identifier "com.yourcompany.AdditionNumbers" 
2012-03-03 12:09:55.909 AdditionNumbers[1698:f803] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 
*** First throw call stack: 
(0x14d1052 0x11bcd0a 0x14bddb8 0x2f0f 0x43064e 0x2abc 0x3689d6 0x3698a6 0x378743 0x3791f8 0x36caa9 0x1bc7fa9 0x14a51c5 0x140a022 0x140890a 0x1407db4 0x1407ccb 0x3692a7 0x36aa9b 0x2a2d 0x29a5) 
terminate called throwing an exceptionCurrent 

を私はそれが可能であるかpickerviewにアルにそれらのデータを表示する必要が下記のように、私は応答を取得していますか?

name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan" 
) 
2012-03-03 12:01:27.865 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan", 
    "\n  Mike Tsalidis" 
) 
2012-03-03 12:01:27.865 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan", 
    "\n  Mike Tsalidis", 
    "\n  Dana Sugarman" 
) 
2012-03-03 12:01:27.866 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan", 
    "\n  Mike Tsalidis", 
    "\n  Dana Sugarman", 
    "\n  Necip Ozyucel" 
) 
2012-03-03 12:01:27.867 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan", 
    "\n  Mike Tsalidis", 
    "\n  Dana Sugarman", 
    "\n  Necip Ozyucel", 
    "\n  Goksel Topbas" 
) 
2012-03-03 12:01:27.868 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan", 
    "\n  Mike Tsalidis", 
    "\n  Dana Sugarman", 
    "\n  Necip Ozyucel", 
    "\n  Goksel Topbas", 
    "\n  Nizar Abdul-Baki" 
) 
2012-03-03 12:01:27.951 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan", 
    "\n  Mike Tsalidis", 
    "\n  Dana Sugarman", 
    "\n  Necip Ozyucel", 
    "\n  Goksel Topbas", 
    "\n  Nizar Abdul-Baki", 
    "\n  Johannes Kanis" 
) 
+0

を呼び出す[materialarray]にデータがないように見えますか? – EmilioPelaez

+0

問題はあなたのピッカーではなく、サーバーからの応答をどのように処理するかについてのコードを投稿してください。エラーは「空の配列の境界を超えたインデックス0」です。明らかにレスポンス配列はゼロまたはゼロです – HelmiB

+0

データをロードする前に 'viewDidLoad'に' [materialarray objectAtIndex:0] 'を呼び出すようです。私はそこに一時的なタイトルを入れて、それを 'connection:didReceiveData:'で修正します。 –

答えて

0

.hファイルにUIPickerViewの代理人を設定しましたか?

あなたはそれが終了したときにアプリが何を言ってんviewDidLoad

関連する問題