如何在两个ViewController之间传递数据,其中第一个VC有两个tableViews,第二个tableViewselect的数据应该传递给第二个VC?

我有一个ViewController中的两个tableViews。 我想将第二个tableView数据传递给第二个ViewController。

现在,每当点击类别时,相关数据将显示在子类别表中。 现在,如果我点击像“身份平面和坚实的数字”SubCategory数据,该单元格标签文本应传递给下一个viewController。 我可以传递数据,但每当点击第二个表格单元格,那不会去下一个ViewController。 问题是我不能去传递数据的下一个视图 。 我遇到了“ DidSelectRowAtIndexPath ”方法的问题。 请帮我解决这个问题,并把代码发给我。

它是我的代码

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

你可以使用一个属性来访问其他类的数据:

 @interface YourSecondView : UIViewController { } @property (nonatomic, retain) NSString* dataString; 

不要忘记它在YourSecondView.m上的@synthesize

在表格视图控制器中,您可以像这样传递数据

  -(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... } 

这是在视图控制器之间传递数据的好方法

由于您有两个表视图,您应该使用tableView:didSelectRowAtIndexPath:方法中的tableView参数来select要传递的正确信息。

尝试这个:

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

如果你想传递数据到其他视图控制器,你可以使用下面的代码

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

在你的viewcontroller中做这个改变,并用这个方法分配init你查看控制器并通过localStr(例如)。 你可以在视图控制器中使用setString