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.}
相关文章推荐
- 谈谈ios传值方式(属性传值、代理传值、Block传值、单例传值、通知传值、KVC传值)
- iOS 传值方法(属性传值、代理传值、Block、通知、单例)
- iOS-OC-iOS传值大全(代理传值,block 传值,单例传值,通知传值,属性传值)
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS 页面间几种传值方式(属性,代理,block,单例,通知
- IOS 页面间传值(属性传值、block传值、代理传值、单例传值、NSNotificaion传值)
- iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS个人整理14-界面间传值:属性传值---代理传值---block传值---单例传值
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS 几种传值方式(属性,代理,block,通知,本地存储,单例类)附Demo
- iOS 几种传值方式(属性,代理,block,通知,本地存储,单例类)附Demo
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- IOS中5种传值方式详解(属性、Block代码块、代理、单例、通知)
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)