2012-01-27 11 views
0

私は2つのビューコントローラを持っています。最初は、テーブルビューでカテゴリを選択します。次のビューでは、前のテーブルビューで選択したカテゴリ内のすべての製品を表示したいと考えています。データベースからphpファイルでデータを取得します。前のテーブルビューから値を取得する方法

これは私の最初のViewController.mです。 'catVal'は私の次のViewControllerに与えたい値です。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    KiesProduct *proView = [[KiesProduct alloc] initWithNibName:@"KiesProduct" bundle:nil]; 
    [self.navigationController pushViewController:proView animated:YES]; 
    NSDictionary *info = [json objectAtIndex:indexPath.row]; 
    NSString *catVal = [info objectForKey:@"Cat_naam"]; 
    [productVC fillArrayProducts:catVal]; 


} 

私の2番目のビューコントローラでは、私は関数fillArayProductsを持っています。

-(void) fillArrayProducts:(NSString *)cat{ 
    NSLog(@"test"); 
    NSMutableString *postString = [NSMutableString stringWithString:kGETProducts]; 
    [postString appendString:[NSString stringWithFormat:@"?%@=%@",@"Pro_cat",cat]]; 
    [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    NSMutableURLRequest *request= [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]]; 
    [request setHTTPMethod:@"POST"]; 

    postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:postString]]; 

    NSError *error; 

    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

    arrayProducts = [[NSMutableArray alloc] init]; 
    for (int i=0; i<[json count]; i++) { 
     NSDictionary *info = [json objectAtIndex:i]; 
     [arrayProducts addObject:[info objectForKey:@"Pro_naam"]]; 
    } 
    NSLog(@"%@",arrayProducts); 

} 

私secondViewController.m

#import <UIKit/UIKit.h> 
#define kGETProducts @"http://localhost/getProducts.php" 
@interface KiesProduct : UITableViewController{ 
    NSMutableArray *json; 
    NSMutableArray *arrayProducts; 
    NSURLConnection *postConnection; 
} 
-(IBAction)add:(id)sender; 
-(void) fillArrayProducts:(NSString *)cat; 
@end 

私はこれを試して '[productVC fillArrayProducts:catVal];'私のテーブルビューDIDの行選択機能では機能しません。誰もがアイデアを持っていますか?

答えて

0

あなたはur second view * proViewを呼び出していますが、代わりにproductVCでfillArrayProductsを呼び出しています。

KiesProduct *proView = [[KiesProduct alloc] initWithNibName:@"KiesProduct" bundle:nil]; 
NSDictionary *info = [json objectAtIndex:indexPath.row]; 
NSString *catVal = [info objectForKey:@"Cat_naam"]; 
[proView fillArrayProducts:catVal]; 
[self.navigationController pushViewController:proView animated:YES]; 
+0

に変更されました。ありがとうございました ! – steaphann

関連する問題