我的应用程序是Tabbar(+导航)应用程序。在FirstViewController中,我调用onModalView。

-(void) onFilter
{
  FilterViewController* controller =[[[FilterViewController alloc] initWithNibName:@"Filter" bundle:[NSBundle mainBundle]] autorelease];
  [self.navigationController presentModalViewController:controller animated:YES];
}

用户可以在FilterViewController中选择许多过滤器。我想将用户首选项转移到FirstViewController。我怎样才能做到这一点 ?

最佳答案

我通常使用委托模式。喜欢:

@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

09-06 18:12