如何在特定的UIViewController被解除时执行操作?

时间:2014-07-20 12:53:11

标签: ios objective-c cocoa-touch uiviewcontroller uistoryboard

我有一个控制器,它以这种方式呈现:

- (void)searchButtonTapped
{
    NSLog(@"search button tapped");
    VAGSearchViewController *svc = [[self storyboard] instantiateViewControllerWithIdentifier:@"searchPageSB"];

    [self presentViewController:svc animated:NO completion:nil];
}

呈现它的控制器是collectionView控制器。我想要做的是让这个collectionView知道什么时候这个searchViewController被解雇,所以我可以执行一个动作。

最有效的方法是什么?

请注意,只有在解除searchViewController时才会执行此操作。

感谢您的时间

1 个答案:

答案 0 :(得分:2)

您可以为此创建自己的代理。

在VAGSearchViewController.h中

@class VAGSearchViewController;
@protocoll VAGSearchViewControllerDismissDelegate <NSObject>
- (void) searchViewControllerDismissed:(VAGSearchViewController*)searchViewController;
@end

//VAGSearchViewController Interface
@property (nonatomic, weak) id<VAGSearchViewControllerDismissDelegate> dismissDelegate;

当你关闭viewController

时,在委托上调用方法
[self.dismissDelegate searchViewControllerDismissed:self];

在你的collectionViewController中添加protocoll

@interface MyCollectionViewController : UICollectionViewController <VAGSearchViewControllerDismissDelegate>

实现didDismiss方法并在呈现searchViewController时将self指定为委托:

- (void)searchButtonTapped
{
    NSLog(@"search button tapped");
    VAGSearchViewController *svc = [[self storyboard] instantiateViewControllerWithIdentifier:@"searchPageSB"];
    svc.dismissDelegate = self;
    [self presentViewController:svc animated:NO completion:nil];
}

- (void) searchViewControllerDismissed:(VAGSearchViewController*)searchViewController{
    // do something here
}