Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

ios - How to pass data between two ViewControllers where first VC having two tableViews, second tableView selected data should pass to second VC?

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];

  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...