最好的方法是使用委托。
//SecVCDelegate.h
#import <Foundation/Foundation.h>
@protocol SecVSDelegate <NSObject>
@optional
- (void)secVCDidDismisWithData:(NSObject*)data;
@end
//SecVC.h
#import <UIKit/UIKit.h>
#import "SecVSDelegate.h"
@interface SecVC : UIViewController
/** Returns the delegate */
@property (nonatomic, assign) id<SecVSDelegate> delegate;
@end
//SecVC.M
...
- (void) dealloc
{
...
delegate = nil
...
}
每当你 popViewControllerAnimated,在它之后(或之前)你这样做
if(_delegate && [_delegate respondsToSelector:@selector(secVCDidDismisWithData:)])
{
[_delegate secVCDidDismisWithData:myDataObject];
}
在 MainVC 中你必须确定你实现了委托函数
//MainVC.m
- (void)secVCDidDismisWithData
{
//do whatever you want with the data
}
为避免任何警告,您必须告诉 MainVC 类实现委托,如下所示:
//MainVC.h
#import "SecVCDelegate.h"
...
@interface MainVC : UIViewController <SecVCDelegate>
...
secVCInstance.delegate = self;
[self.navigationController pushViewController:secVCInstance];
...