iOS代理反向传值

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

//把第二个页面的值传给第一个页面 反向传值 使用代理
//在第二个页面设置代理
//.h文件
#import <UIKit/UIKit.h>

//我们可以把协议写在主动方的类中
@protocol ChangeStatus <NSObject>
//代理方法
- (void)changeStatus:(NSString *)isOn;
@end


@interface SecondViewController : UIViewController

//设置代理  弱引用
@property (nonatomic,assign) id <ChangeStatus> delegate;


@end


//.m文件
#import "SecondViewController.h"
@interface SecondViewController ()

@end

@implementation SecondViewController


- (void)viewDidLoad
{
    
    [super viewDidLoad];
    [self changeStatus];
    
}

#pragma mark - 查看代码是否实现
- (void)changeStatus{
    //检测代理有没有实现changeStatus:方法
    if([self.delegate respondsToSelector:@selector(changeStatus:)]){
        [self.delegate changeStatus:@"123"];
    }else{
        NSLog(@"代理没有实现changeStatus:方法");
    }
    
}
- (void)btnClick:(UIButton *)button{
    //返回
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

//第一个页面
//.h文件
#import <UIKit/UIKit.h>
//导入头文件
#import "SecondViewController.h"
//遵守协议  作为第二张的代理
@interface RootViewController : UIViewController <ChangeStatus>

@end


//.m文件

#import "RootViewController.h"
@interface RootViewController ()
{
    SecondViewController *_svc;
}
@end

@implementation RootViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
   //主动方往往 创建在被动方里面
    _svc = [[SecondViewController alloc] init];
    //设置代理
    _svc.delegate = self;
}
//代理要实现的方法
- (void)changeStatus:(NSString *)isOn{
     NSLog(@"%@",isOn);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end