如何将数据从模态 ViewController 传输到父 ViewController?
Posted
技术标签:
【中文标题】如何将数据从模态 ViewController 传输到父 ViewController?【英文标题】:How do I transmit datas from modal ViewController to parent ViewController? 【发布时间】:2011-10-24 11:23:39 【问题描述】:我的应用是标签栏(+导航)应用。在 FirstViewController 我调用 onModalView。
-(void) onFilter
FilterViewController* controller =[[[FilterViewController alloc] initWithNibName:@"Filter" bundle:[NSBundle mainBundle]] autorelease];
[self.navigationController presentModalViewController:controller animated:YES];
FilterViewController 中有很多过滤器可供用户选择。我想将用户偏好传输到 FirstViewController。我怎样才能做到这一点 ?
【问题讨论】:
【参考方案1】:我通常使用委托模式。喜欢:
@class FilterViewController;
@protocol FilterViewControllerDelegate
@required
- (void)filterViewController:(FilterViewController *)controller didSelectFilters:(NSInteger)filters;
@end
@interface FilterViewController : UIViewController
id<FilterViewControllerDelegate> _delegate;
@property (nonatomic, assign) id<FilterViewControllerDelegate> delegate;
@end
在你的FirstViewController
:
-(void) onFilter
FilterViewController* controller =[[[FilterViewController alloc] initWithNibName:@"Filter" bundle:[NSBundle mainBundle]] autorelease];
controller.delegate = self;
[self.navigationController presentModalViewController:controller animated:YES];
- (void)filterViewController:(FilterViewController *)controller didSelectFilters:(NSInteger)filters
// Do something
在您的 FilterViewController
调用 - (void)filterViewController:(FilterViewController *)controller didSelectFilters:(NSInteger)filters
之前解除代理。
【讨论】:
以上是关于如何将数据从模态 ViewController 传输到父 ViewController?的主要内容,如果未能解决你的问题,请参考以下文章