iOS - 代理的封装用法
把代理写进一个类里面,这个类.h不声明,所以.m也就不用实现。既然不实现,那留着它也就无意义了,所以,我把它删了。
流程就是这样 : viewController-->FirstViewController-->SecondViewController,其中返回的过程中,Second 会 传值给 First, 而 First 则会给 ViewController 传值。依次看代码就好:
//
// ZJJPassTheNumberDelegate.h
// ProjectZJJ
//
// Created by ZJJ on 2017/8/4.
// Copyright © 2017年 ZJJ. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol ZJJPassTheNumberDelegate <NSObject>
- (void)passTheNumberWithNum:(int)num;
@end
//
// ViewController.h
// ProjectZJJ
//
// Created by ZJJ on 2017/8/3.
// Copyright © 2017年 ZJJ. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// Created by ZJJ on 2017/8/3.
// Copyright © 2017年 ZJJ. All rights reserved.
//
#import "ViewController.h"
#import "FirstViewController.h"
@interface ViewController () <ZJJPassTheNumberDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor cyanColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
FirstViewController *first = [[FirstViewController alloc] init];
first.delegate = self;
[self presentViewController:first animated:YES completion:nil];
}
- (void)passTheNumberWithNum:(int)num {
NSLog(@"第一个类传过来的值为:%d",num);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// FirstViewController.h
// ProjectZJJ
//
// Created by ZJJ on 2017/8/4.
// Copyright © 2017年 ZJJ. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "ZJJPassTheNumberDelegate.h"
@interface FirstViewController : UIViewController
@property (nonatomic, assign) id <ZJJPassTheNumberDelegate> delegate;
@end
//
// FirstViewController.m
// ProjectZJJ
//
// Created by ZJJ on 2017/8/4.
// Copyright © 2017年 ZJJ. All rights reserved.
//
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController () <ZJJPassTheNumberDelegate>
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 100, 50);
button.backgroundColor = [UIColor cyanColor];
[button setTitle:@"返回" forState:UIControlStateNormal];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)back {
if ([self.delegate respondsToSelector:@selector(passTheNumberWithNum:)]) {
[self.delegate passTheNumberWithNum:10];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
SecondViewController *second = [[SecondViewController alloc] init];
second.protocal = self;
[self presentViewController:second animated:YES completion:nil];
}
- (void)passTheNumberWithNum:(int)num {
NSLog(@"第二个类传过来的值为:%d",num);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// SecondViewController.h
// ProjectZJJ
//
// Created by ZJJ on 2017/8/4.
// Copyright © 2017年 ZJJ. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "ZJJPassTheNumberDelegate.h"
@interface SecondViewController : UIViewController
@property (nonatomic, assign) id <ZJJPassTheNumberDelegate> protocal;
@end
//
// SecondViewController.m
// ProjectZJJ
//
// Created by ZJJ on 2017/8/4.
// Copyright © 2017年 ZJJ. All rights reserved.
//
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor lightGrayColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if ([self.protocal respondsToSelector:@selector(passTheNumberWithNum:)]) {
[self.protocal passTheNumberWithNum:100];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end