-1

I have two tableViews in a ViewController. I want to pass the second tableView data to second ViewController.

Now whenever clicking on Category, that related data will display in SubCategory table. Now if i click on SubCategory data like "Identity planar and solid figures", that cell label text should be pass to next viewController. i can pass data but whenever clicking on Second table cell, that is not going to next ViewController. The problem is i am not able to go to next View with passing data. I am getting problem at "DidSelectRowAtIndexPath" method. Please help me on this problem and send me the code.

Its my code

-(void)viewDidLoad

{


NSMutableArray *Mute_Category=[[NSMutableArray alloc]initWithObjects:@"Geometry", @"Mixed operations", nil];

NSMutableArray *Mute_Sub_Category=[[NSMutableArray alloc]initWithObjects:@"Identify planar and solid figures", @"Open and closed shapes and qualities of polygons", @"Nets of 3-dimensional figures", @"Types of angles", nil];

tag=1;

[table_category reloadData];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{

    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{

    if (tag==1)
    {
        return [Mute_category count];
    }
    else
    {
        return [Mute_Sub_Category count];
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{

    cell=[[UITableViewCell alloc]init];

    if (tag==1)
    {
        cell.textLabel.text=[Mute_category objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text=[Mute_Sub_Category objectAtIndex:indexPath.row];
    }

    return cell;


}

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

        NSString *localStr=[Mute_category objectAtIndex:indexPath.row];

        tag=2;

        [table_sub_category reloadData];

  }
Ramesh Rayadurgam
  • 75
  • 1
  • 1
  • 10
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Bishal Ghimire Oct 04 '13 at 09:42

3 Answers3

1

You can use a property to access the data from other classes:

@interface YourSecondView : UIViewController {
}

@property (nonatomic, retain) NSString* dataString;

don't forget it to @synthesize on YourSecondView.m

In the table View Controller you can pass data like that

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

{

       YourSecondView * SV = [[YourSecondView alloc]init];
       sv.dataString = [Mute_category objectAtIndex:indexPath.row];

       // here write what you want to do next...

  }

Here is good explination for passing data between view controllers

Community
  • 1
  • 1
Bug
  • 2,576
  • 2
  • 21
  • 36
0

Since you have two table views, you should be using the tableView parameter in the tableView:didSelectRowAtIndexPath: method to select the correct information to pass on.

Abizern
  • 146,289
  • 39
  • 203
  • 257
0

Try this:

if (tableView.tag==YOUR TAG)// Give the tag of table for which your need to push view controller
{
    NSString *localStr=[Mute_category objectAtIndex:indexPath.row];

YourViewController *obj =[[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
}

if you want to pass data to other view controller you can use the below code

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil YourData:(NSString *)yourData
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    NSString *setString=yourData;
}
return self;
}

Make this changes in your viewcontroller and alloc init you view controller with this method and pass localStr (for example). You can use setString in your view controller

Tushar
  • 88
  • 7