代理

#import "ViewController.h"
#import "ModalViewController.h"
/*
    传值:1.只要能拿到对方就能传值
 
    顺传:给需要传值的对象,直接定义属性就能传值
    逆传:用代理,block,就是利用block去代替代理
 */

@interface ViewController ()<ModalViewControllerDelegate>

@end

@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    ModalViewController *modalVc = [[ModalViewController alloc] init];
    modalVc.view.backgroundColor = [UIColor brownColor];
    modalVc.delegate = self;
    // 跳转
    [self presentViewController:modalVc animated:YES completion:nil];
}
#pragma mark - ModalViewControllerDelegate
// 传值给ViewController
- (void)modalViewController:(ModalViewController *)modalVc sendValue:(NSString *)value
{
    NSLog(@"%@",value);
}
@end


#import <UIKit/UIKit.h>
@class ModalViewController;
@protocol ModalViewControllerDelegate <NSObject>
@optional
// 设计方法:想要代理做什么事情
- (void)modalViewController:(ModalViewController *)modalVc sendValue:(NSString *)value;
@end
@interface ModalViewController : UIViewController
@property (nonatomic, weak) id<ModalViewControllerDelegate> delegate;
@end

#import "ModalViewController.h"
@interface ModalViewController ()
@end
@implementation ModalViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 传值给ViewController
    if ([_delegate respondsToSelector:@selector(modalViewController:sendValue:)]) {
        [_delegate modalViewController:self sendValue:@"123"];
    }
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容