`
iaiai
  • 浏览: 2146026 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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];
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics