[英]Two view controllers use same table view
我是iOS开发的新手。 我想知道如何将数据从同一tableview传递回两个不同的view controller。
这是我的didselectrow方法:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
sendarea = [tableData objectAtIndex:indexPath.row];
Step2ClassifiedViewController *parentViewC = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
parentViewC.areareceive = sendarea;
[self.navigationController popViewControllerAnimated:YES];
}
但这仅是一种观点。 如果我使用同一张表,我还有另一种观点也需要传回数据。 那可能吗? 还是我只需要创建另一个表?
您可以使用同一张表。 只有您需要更改数据源。 并且可以在每个didSelectRowAtIndexpath中调用
就像,出租车司机乘坐他的出租车载客。 出租车空了时,他可以载客。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
sendarea = [tableData objectAtIndex:indexPath.row];
if(state == 1){
Step2ClassifiedViewController *parentViewC = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
parentViewC.areareceive = sendarea;
[self.navigationController popViewControllerAnimated:YES];
}
else if (state == 2){
//get your other View Controller and set data before pop to that view controller.
NSArray *viewControllers = [[self navigationController] viewControllers];
for(UIViewController *vc in viewControllers){
if([vc isKindOfClass:[OtherViewController class]]){
OtherViewController *otherVC = (OtherViewController*)vc;
otherVC.areareceive = sendarea;
[[self navigationController] popToViewController:otherVC animated:YES];
}
}
}
else{
//load any View Controller you want
}
}
根据您的问题和评论,我假设您要基于不同的状态加载不同的View Controller并传递数据。
上面是这样做的一种方法。 但是,您也可以将委托用于您的目的,这将是一个很好的编程实践。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.