您的位置:首页 > 移动开发 > IOS开发

ios 属性、通知、block、代理、单例传值

2018-03-09 12:01 399 查看


viewcontroller.m
@interface ViewController ()<NextViewControllerDelegate>
@property (nonatomic, strong) UILabel *label;@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *back;@end
@implementation ViewController
- (UILabel *)label {    if (_label == nil) {        _label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 30)];        _label.backgroundColor = [UIColor orangeColor];        _label.textColor = [UIColor cyanColor];        _label.font = [UIFont systemFontOfSize:20];        _label.text =@"穿穿穿穿";            }    return _label;}
- (UIButton *)button {    if (_button == nil) {        _button = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];        _button.backgroundColor = [UIColor purpleColor];        [_button setTitle:@"NEXT" forState:UIControlStateNormal];        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];        [_button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];    }    return _button;}
- (UILabel *)back {    if (_back == nil) {        _back = [[UILabel alloc] initWithFrame:CGRectMake(100, 300, 200, 40)];        _back.backgroundColor = [UIColor cyanColor];        _back.textColor = [UIColor whiteColor];            }    return _back;}
- (void)button:(UIButton *)sender {    NextViewController *next = [[NextViewController alloc] init];    //一 、1.属性传值,将label的值传过去,正向传值    next.strValue = self.label.text;    //二、单例传值,可正、反向传值、跨页面传值    [DefaultSingle shareSingle].str = self.label.text;    //三、4、签代理    next.delegate = self;    //四、block接收block回调    next.block = ^(NSString *str) {        NSLog(@"block返回的值");    };    //五、监听    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notice:) name:@"noticeName" object:nil];    ;    [self presentViewController:next animated:YES completion:nil];}- (void)notice:(NSNotification *)notice {    NSLog(@"接收通知的值==%@", notice.userInfo[@"key"]);}
//三、5、实现协议方法- (void)passValue:(NSString *)str {    NSLog(@"代理");}

- (void)viewDidLoad {    [super viewDidLoad];                [self.view addSubview:self.label];    [self.view addSubview:self.button];    //反向传值接收显示    [self.view addSubview:self.back];        NSLog(@"sfsdfs");        // Do any additional setup after loading the view, typically from a nib.}
======================================DefaultSingle.h@interface DefaultSingle : NSObject
@property (nonatomic, copy) NSString *str;
+ (instancetype)shareSingle;
@end
DefaultSingle.m
@implementation DefaultSingle//二、通过类方法保证类中只有一个对象+ (instancetype)shareSingle {    static DefaultSingle *single = nil;    if (single == nil) {        single = [[DefaultSingle alloc] init];    }    return single;}

@end====================================NextViewController.h
#import <UIKit/UIKit.h>//三、1、声明协议方法@protocol NextViewControllerDelegate <NSObject>
- (void)passValue:(NSString *)str;
@end
@interface NextViewController : UIViewController

//三、2、协议方法接口,外部可以拿到@property (nonatomic, strong)id<NextViewControllerDelegate>delegate;//block反向传值@property (copy) void(^block)(NSString *);
@property (nonatomic, copy) NSString *strValue;//一、2.用来承接传过来的值
@end
NextViewController.m
#import "NextViewController.h"#import "DefaultSingle.h"
@interface NextViewController ()
@property (nonatomic, strong) UITextField *textField;@property (nonatomic, strong) UIButton *btn;
@end
@implementation NextViewController
- (UITextField *)textField {    if (_textField == nil) {        _textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];        _textField.backgroundColor = [UIColor purpleColor];        //_textField.textColor = [UIColor yellowColor];        _textField.borderStyle = UITextBorderStyleLine;        //一、3.属性传值显示        _textField.text = self.strValue;    }    return _textField;}
- (UIButton *)btn {    if (_btn == nil) {        _btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];        _btn.backgroundColor = [UIColor greenColor];        [_btn setTitle:@"返回" forState:UIControlStateNormal];        [_btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];        [_btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];        _btn.titleLabel.font = [UIFont systemFontOfSize:20];    }    return _btn;}- (void)btn:(UIButton *)sender {    //三、3、让代理实现协议方法    [self.delegate passValue:@"代理传值"];    //二、单例传值接收    _textField.text = [DefaultSingle shareSingle].str;    //四、block传值    self.block(@"block传值");    //五、发送通知    [[NSNotificationCenter defaultCenter] postNotificationName:@"noticeName" object:nil userInfo:@{@"key":@"通知传值"}];    [self dismissViewControllerAnimated:YES completion:nil];}
- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];        [self.view addSubview:self.textField];    [self.view addSubview:self.btn];        // Do any additional setup after loading the view.}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐