#import <Foundation/Foundation.h> @protocol ValueDelegate <NSObject> -(void)setValue:(NSString*)value; @end
第一个OneViewController.h
#import "BaseTableViewController.h" #import "AppDelegate.h" @interface OneViewController : UiViewController<ValueDelegate> @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 <UIKit/UIKit.h> #import "AppDelegate.h" @interface TwoViewController : UIViewController //这里用assign而不用retain是为了防止引起循环引用。 @property(nonatomic,assign) NSObject<ValueDelegate> *delegate; @end
第二个界面TwoViewController.m
[self.delegate setValue:@"委托"]; [self.navigationController popViewControllerAnimated:YES];