传值代理block首页投稿(暂停使用,暂停投稿)通知——NSNotification

单例传值、block传值、代理传值与通知中心传值

2016-03-17  本文已影响985人  左左4143

最近想整理一下页面间传值的方法汇总,所以写了这个demo ,在这里与大家分享

在这个demo中我们需要建两个控制器 从第一个控制器可以模态到第二个控制器中,两个控制器中各有一个TextField, 如果从第一个页面向第二个页面传值可以使用属性的方法,但是从第二个向第一个页面传就会失效,可以用以下这四种方式:


单例传值

单例传值是利用了单例的生命周期为整个程序这一特点来进行传值的

.h文件内

@interface DataManager : NSObject

+(instancetype)Default;

@property(nonatomic,strong)NSString *dataManagerString;

@end

.m文件内

#import "DataManager.h"

static DataManager *manager = nil;

@implementation DataManager

+(instancetype)Default
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[DataManager alloc]init];
    });
    return manager;

}

@end
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //给单例赋值
    [DataManager Default].dataManagerString = self.firstField.text;
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
-(void)viewWillAppear:(BOOL)animated{

    //利用单例传值
    self.field.text = [DataManager Default].dataManagerString;
    

}

block传值

block传值是利用block函数回调这一功能来实现的

#import <UIKit/UIKit.h>

typedef void(^passValueBlock)(NSString *blockString);

@interface SecondViewController : UIViewController

@property(nonatomic,copy)passValueBlock block;

@end
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    //调用block
    self.block(self.secondField.text);
    
    [self dismissViewControllerAnimated:YES completion:nil];

}
//进入第二个页面的点击事件
- (IBAction)blockButton:(id)sender {
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    
    //block传值
    secondVC.block = ^(NSString *string){
        
        self.field.text = string;
    };
    
    [self presentViewController:secondVC animated:YES completion:nil];
    
}

代理传值

#import <Foundation/Foundation.h>

@protocol passValueDelegate <NSObject>

-(void)passString:(NSString *)string;

@end

#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController

@property(nonatomic,weak)id<passValueDelegate>delegate;

@end
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //代理传值
    [self.delegate passString:self.thirdField.text];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
//点击进入第二个界面的方法
- (IBAction)delegateButton:(id)sender {
    ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
    thirdVC.delegate = self;
    [self presentViewController:thirdVC animated:YES completion:nil];
}

//代理方法的实现
-(void)passString:(NSString *)string{

    self.field.text = string;

}

通知中心传值

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    //通知中心传值
    NSString *string = self.fourthField.text;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"passValue" object:string];
    
    [self dismissViewControllerAnimated:YES completion:nil];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.field = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];
    self.field.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.field];
    
    //设置通知中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passAction:) name:@"passValue" object:nil];
    
}
//通知中心执行的方法
- (void)passAction:(NSNotification *)noti{
    
    self.field.text = noti.object;
    
}
//移除通知中心
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

需要的朋友请点击这里下载源代码参考

上一篇下一篇

猜你喜欢

热点阅读