Ios 如何在两个ViewController之间传递数据,其中第一个VC有两个tableView,第二个tableView所选数据应传递给第二个VC?
我在一个视图控制器中有两个表视图。我想将第二个tableView数据传递给第二个ViewController 现在,只要单击“类别”,相关数据就会显示在“子类别”表中。现在,若我单击子类别数据,如“标识平面和实体图形”,那个么该单元标签文本应该传递给下一个viewController。我可以传递数据,但只要单击第二个表格单元格,就不会传递到下一个ViewController。问题是我无法通过传递数据转到下一个视图。 我在“DidSelectRowAtIndexPath方法中遇到问题。 请帮我解决这个问题,并将代码发送给我 这是我的密码Ios 如何在两个ViewController之间传递数据,其中第一个VC有两个tableView,第二个tableView所选数据应传递给第二个VC?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在一个视图控制器中有两个表视图。我想将第二个tableView数据传递给第二个ViewController 现在,只要单击“类别”,相关数据就会显示在“子类别”表中。现在,若我单击子类别数据,如“标识平面和实体图形”,那个么该单元标签文本应该传递给下一个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];
}
由于您有两个表视图,因此应该使用
tableView:didSelectRowAtIndexPath:
方法中的tableView
参数来选择要传递的正确信息。尝试以下操作:
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和alloc init中进行此更改,然后使用此方法查看控制器并传递localStr(例如)。您可以在视图控制器中使用setString您可以使用属性从其他类访问数据:
@interface YourSecondView : UIViewController {
}
@property (nonatomic, retain) NSString* dataString;
不要忘记在YourSecondView.m
在表视图控制器中,可以这样传递数据
-(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...
}
可能存在的副本