0
点赞
收藏
分享

微信扫一扫

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


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

#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];

举报

相关推荐

0 条评论