ios用委托实现第二个界面返回给前一个界面数据

定义一个委托AppDelegate.h文件:

#import

@protocol ValueDelegate

-(void)setValue:(NSString*)value;

@end

第一个OneViewController.h

#import "BaseTableViewController.h"
#import "AppDelegate.h"

@interface OneViewController : UiViewController

@end

第一个界面OneViewController.m,实现setValue方法

-(void)setValue:(NSString*)value{
...
}
//在打开第二个界面的地方如下
-(void)open{
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"ui" bundle:nil];
TwoViewController* controller = [mainStoryboard instantiateViewControllerWithIdentifier:@"TwoViewController"];
controller.delegate = self;
[self.navigationController pushViewController:controller animated:YES];
}

第二个界面TwoViewController.h

#import
#import "AppDelegate.h"

@interface TwoViewController : UIViewController

//这里用assign而不用retain是为了防止引起循环引用。
@property(nonatomic,assign) NSObject *delegate;

@end

第二个界面TwoViewController.m

[self.delegate setValue:@"委托"];
[self.navigationController popViewControllerAnimated:YES];

你可能感兴趣的:(mac/IOS)