UITableView:popViewController 并将行索引保留到父控制器中?

2024-05-03

我有以下配置:

一个视图控制器父控制器包含一个TableView父表带有自定义单元格,每个单元格显示 2 个标签。

一个视图控制器子控制器包含一个TableView子表。当用户单击controllerParent的某个单元格时,会显示此视图,childTable内容取决于所选的parentController单元格。 我用这个方法:

[self.navigationController pushViewController:controleurEnfant animated:YES];

现在,当我单击 childTable 中的单元格时,我会返回到之前的视图:

[self.navigationController popViewControllerAnimated:YES];

当然,我可以轻松获取所选子表行的索引。但我唯一不知道的是当我回到那里时如何保留这些数据以便在parentController中使用它?

感谢您的帮助...


对于此类问题可以使用委托

代码来自RayWenderlichs 教程 http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2:

.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

childViewController 中的 .m

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

这是一些关于委托模式的文档:代表和数据来源 https://developer.apple.com/library/ios/#documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UITableView:popViewController 并将行索引保留到父控制器中? 的相关文章

随机推荐