-1

I have done for parsing data from JSON and put it show in Table view. So I want to pass data (catID and catName )to another Page it's DetailPageController how can I do like that? the below is my code. Thanks in advance!

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *myObject;
    // A dictionary object
    NSDictionary *dictionary;
    // Define keys
    NSString *catName;
    NSString *catID;



}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    catName =@"Category Name";
    catID=@"Category ID";

    myObject = [[NSMutableArray alloc] init];

    NSData *jsonSource = [NSData dataWithContentsOfURL:
                          [NSURL URLWithString:@"MY_URL"]];

    id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                      jsonSource options:NSJSONReadingMutableContainers error:nil];

    for (NSDictionary *dataDict in jsonObjects) {
        NSString *TheCatName = [dataDict objectForKey:@"cat_name"];
        NSString *TheCatID = [dataDict objectForKey:@"cat_id"];


        NSLog(@"cat_name: %@",TheCatName);
        NSLog(@"cat_id: %@",TheCatID);



        dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                      TheCatName, catName,
                      TheCatID, catID,

                      nil];
        [myObject addObject:dictionary];
    }



}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return myObject.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Item";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[UITableViewCell alloc]initWithStyle:
              UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }


    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];

    NSMutableString *textcat;

            textcat = [NSMutableString stringWithFormat:@"%@",
            [tmpDict objectForKeyedSubscript:catName]];

    NSMutableString *catid;
    catid = [NSMutableString stringWithFormat:@"ID: %@ ",
              [tmpDict objectForKey:catID]];

    cell.textLabel.text = textcat;
    cell.detailTextLabel.text= catid;

    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];


    NSMutableString *catID;
    catID = [NSMutableString stringWithFormat:@"%@",
             [tmpDict objectForKeyedSubscript:cat_id]];


    NSLog(@"didSelectRowAtIndexPath");
    /*UIAlertView *messageAlert = [[UIAlertView alloc]
     initWithTitle:@"Row Selected" message:@"You've selected a row" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];*/
    UIAlertView *messageAlert = [[UIAlertView alloc]
                                 initWithTitle:@"Row Selected" message:catID delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    // Display the Hello World Message
    [messageAlert show];

    // Checked the selected row
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    [tableView deselectRowAtIndexPath:indexPath animated:YES];



   }
xDevelopers
  • 105
  • 1
  • 2
  • 9

1 Answers1

0

First import DetailPageController in TableviewController Class with help of following line.

#import "DetailPageController.h"

Now implement following method in your TableViewController Class

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailPageController * detailPageControllerOBJ_ = [[ImageOptionViewController alloc]initWithNibName:@“XIBName bundle:nil];
    detailPageControllerOBJ_.dicSelectedData = [self.arrayData objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailPageControllerOBJ_ animated:YES];
}

And in DetailPageController.h put following code.

@property (nonatomic,strong) NSDictionary * dicSelectedData;

You will get your selected data in dicSelectedData Dictionary

Abhishek Sharma
  • 3,283
  • 1
  • 13
  • 22
  • I do follow your answer but it's not working could you please check my post again I use didSelectRowAtIndexPath how to send the value and go to another viewcontroller – xDevelopers Dec 11 '14 at 11:20
  • what is your rootViewController a NavigationController or ViewController. You can check that in AppDelegate file. I seen your question but you are not pushing to New Viewcontroller, the same thing i told you to do. Create object of DetailViewController and navigate to it. – Abhishek Sharma Dec 11 '14 at 12:03