我有以下配置:
一个ViewController parentController,其中包含一个带有自定义单元格的TableView parentTable,以在每个单元格中显示2个标签。
一个包含TableView childTable的ViewController childController。当用户单击controllerParent的单元格,并且childTable的内容取决于所选parentController单元格时,将显示此视图。
我用这种方法:
[self.navigationController pushViewController:controleurEnfant animated:YES];
现在,当我单击childTable中的一个单元格时,我将回到此先前的视图:
[self.navigationController popViewControllerAnimated:YES];
当然,我可以轻松获得所选childTable的行的索引。但是我唯一不知道的是,当我回到那里时,如何将这些数据保留在parentController中使用?
谢谢你的帮助...
最佳答案
对于这种问题,您可以使用委托
来自RayWenderlichs Tutorial的代码:
.h在您的childViewController中:
@class ChildViewController;
@protocol ChildViewControllerDelegate <NSObject>
- (void)childViewControllerDidSelect:(id)yourData;
@end
@interface childViewController : UITableViewController
@property (nonatomic, weak) id <ChildViewControllerDelegate> delegate;
- (IBAction)cancel:(id)sender;
- (IBAction)done:(id)sender;
@end
.m在childViewController中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.delegate childViewControllerDidSelect:myObject];
[self.navigationController popViewControllerAnimated:YES];
}
在您的parentViewController.h中采用协议
@interface ParentViewController : UITableViewController <ChildViewControllerDelegate>
并实现委托方法
- (void)childViewControllerDidSelect:(id)yourData
{
self.someProperty = yourData
}
并且不要忘记在按下之前设置委托:
...
ChildViewController *vc = [ChildViewController alloc] init];
vc.delegate = self;
[self.navigationController pushViewController:vc animated:YES];
这是有关授权模式的一些文档:Delegates and Data Sources
关于objective-c - UITableView:popViewController并保持行索引到父 Controller ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10767075/