1. 程式人生 > >ios 屬性、通知、block、代理、單例傳值

ios 屬性、通知、block、代理、單例傳值

viewcontroller.m

@interfaceViewController ()<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 = [[

UILabelalloc] initWithFrame:CGRectMake(100, 100, 200, 30)];

_label.backgroundColor = [UIColororangeColor];

_label.textColor = [UIColorcyanColor];

        _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 = [UIColorpurpleColor];

        [_buttonsetTitle:@"NEXT"forState:UIControlStateNormal];

        [_buttonsetTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];

        [_buttonaddTarget:selfaction:@selector(button:) forControlEvents:

UIControlEventTouchUpInside];

    }

return_button;

}

- (UILabel *)back {

    if (_back == nil) {

_back = [[UILabelalloc] initWithFrame:CGRectMake(100, 300, 200, 40)];

_back.backgroundColor = [UIColorcyanColor];

_back.textColor = [UIColorwhiteColor];

    }

return_back;

}

- (void)button:(UIButton *)sender {

NextViewController *next = [[NextViewControlleralloc] init];

//一 、1.屬性傳值,將label的值傳過去,正向傳值

    next.strValue = self.label.text;

//二、單例傳值,可正、反向傳值、跨頁面傳值

    [DefaultSingleshareSingle].str = self.label.text;

//三、4、籤代理

    next.delegate = self;

//四、block接收block回撥

    next.block = ^(NSString *str) {

NSLog(@"block返回的值");

    };

//五、監聽

    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(notice:) name:@"noticeName"object:nil];

    ;

    [selfpresentViewController:next animated:YEScompletion:nil];

}

- (void)notice:(NSNotification *)notice {

    NSLog(@"接收通知的值==%@", notice.userInfo[@"key"]);

}

//三、5、實現協議方法

- (void)passValue:(NSString *)str {

    NSLog(@"代理");

}

- (void)viewDidLoad {

    [superviewDidLoad];

    [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"

@interfaceNextViewController ()

@property (nonatomic, strong) UITextField *textField;

@property (nonatomic, strong) UIButton *btn;

@end

@implementation NextViewController

- (UITextField *)textField {

    if (_textField == nil) {

_textField = [[UITextFieldalloc] initWithFrame:CGRectMake(100, 100, 200, 40)];

_textField.backgroundColor = [UIColorpurpleColor];

//_textField.textColor = [UIColor yellowColor];

_textField.borderStyle = UITextBorderStyleLine;

//一、3.屬性傳值顯示

_textField.text = self.strValue;

    }

return_textField;

}

- (UIButton *)btn {

    if (_btn == nil) {

_btn = [[UIButtonalloc] initWithFrame:CGRectMake(100, 200, 200, 40)];

_btn.backgroundColor = [UIColorgreenColor];

        [_btnsetTitle:@"返回"forState:UIControlStateNormal];

        [_btnsetTitleColor:[UIColorredColor] forState:UIControlStateNormal];

        [_btnaddTarget:selfaction:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];

_btn.titleLabel.font = [UIFontsystemFontOfSize:20];

    }

return_btn;

}

- (void)btn:(UIButton *)sender {

//三、3、讓代理實現協議方法

    [self.delegate passValue:@"代理傳值"];

//二、單例傳值接收

_textField.text = [DefaultSingleshareSingle].str;

//四、block傳值

self.block(@"block傳值");

//五、傳送通知

    [[NSNotificationCenterdefaultCenter] postNotificationName:@"noticeName"object:niluserInfo:@{@"key":@"通知傳值"}];

    [selfdismissViewControllerAnimated:YEScompletion:nil];

}

- (void)viewDidLoad {

    [superviewDidLoad];

self.view.backgroundColor = [UIColorwhiteColor];

    [self.view addSubview:self.textField];

    [self.view addSubview:self.btn];

// Do any additional setup after loading the view.

}