关于iOS往回传值的函数,比较基础也比较常用的三个——代理、通知、block块传值

关于iOS往回传值的函数,比较基础也比较常用的三个——代理、通知、block块传值。

本文使用了四个控制器,一个是导航控制器。为了简单化,使用storyboard拖线push方式切换控制器。也为了偷懒一个项目展示三种回传值方式:


关于iOS往回传值的函数,比较基础也比较常用的三个——代理、通知、block块传值_第1张图片
storyboard.png
RZNextViewController.h

//第二个控制器的.h文件

#import 
@class RZNextViewController;
@protocol RZNextViewControllerDelegate 
@optional
- (void)nextViewController:(RZNextViewController *)nextViewController withTitle:(NSString *)title;
@end

@interface RZNextViewController : UIViewController

@property (nonatomic,weak) iddelegate;

@property (copy, nonatomic) void (^strBlock)(NSString*);

@end
RZNextViewController.m

//第二个控制器的.m文件

#import "RZNextViewController.h"

@interface RZNextViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;//代理传值文本框

@property (weak, nonatomic) IBOutlet UITextField *textField1;//通知传值文本框

@property (weak, nonatomic) IBOutlet UITextField *textField2;//block传值文本框

@end

@implementation RZNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

//代理回调按钮点击
- (IBAction)backButtonClick:(id)sender {
    
    NSString *backTitle = self.textField.text;
    
    if ([self.delegate respondsToSelector:@selector(nextViewController:withTitle:)]) {
        [self.delegate nextViewController:self withTitle:backTitle];
    }
    
    [self.navigationController popToRootViewControllerAnimated:YES];  
}


//通知回调按钮点击
- (IBAction)back1ButtonClick:(id)sender {
    
    NSNotificationCenter *noCenter = [NSNotificationCenter defaultCenter];
    
    [noCenter postNotificationName:@"huidiao" object:self.textField1.text];
    
    [self.navigationController popToRootViewControllerAnimated:YES];  
}

//block块回调按钮点击
- (IBAction)blockButtonClick:(id)sender {
    
    if (self.strBlock) {
        self.strBlock(self.textField2.text);
    }
    
    [self.navigationController popViewControllerAnimated:YES];    
}

@end
接着是重点了,在根控制器中分别实现三种方式回传时赋值操作。
其中block块赋值方式虽然写在,push调用的方法中,在block块传值的第二个页面控制器回调时,程序会直接跳入- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 方法内部,直接执行赋值的部分。
#import "ViewController.h"
#import "RZNextViewController.h"

@interface ViewController ()
//显示回传值的文本框
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@end

@implementation ViewController

//push时调用该方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    RZNextViewController *nextVC = segue.destinationViewController;
    
    nextVC.delegate = self;
    //block块回传时,赋值调用
    nextVC.strBlock = ^(NSString *text){
        self.textLabel.text = text;
    };
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     NSNotificationCenter *noCenter = [NSNotificationCenter defaultCenter];

     [noCenter addObserver:self selector:@selector(show:) name:@"huidiao" object:nil];
    
}

- (void)show:(NSNotification *)notification{

    NSString *str = notification.object;
    
    self.textLabel.text = str;
}

//实现代理方法
- (void)nextViewController:(RZNextViewController *)nextViewController withTitle:(NSString *)title{
    self.textLabel.text = title;
}

你可能感兴趣的:(关于iOS往回传值的函数,比较基础也比较常用的三个——代理、通知、block块传值)