UI控件笔记(七):UI之五种传值方式 代理\单例\缓存\通知\正向

一、传值简介


AAAA 代理: 1V1       A->BBA的时候使用

BBBB    单例: 无所谓     整个应用都要用的数据,存一个应用内的全局值,ios6ios7

CCCC 缓存: 为所谓     整个应用都要用的数据,保证下次启动还有点数据,账号、密码

DDDD 通知: 1V      犯懒的时候,A发通知,B观察通知,BA之前,比较占资源

E  正向传值:A->BB的内容由A来定,那么B应该有一个属性,A->B的时候给B的属性赋值


二、举例


1、AppDelegate.m文件中的写法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor whiteColor];

    

    FirstViewController *first = [[FirstViewController alloc] init];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:first];

    [first release];

    self.window.rootViewController = nav;

    [nav release];

    

    [self.window makeKeyAndVisible];

    return YES;

}


2FirstViewController.m文件中的写法

#import "FirstViewController.h"

#import "SecondViewController.h"

#import "SingleTon.h"


@interface FirstViewController ()<SecondDelegate>//AAAA 代理声明可以写这里


@end


@implementation FirstViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor redColor];

    //DDDD 通知中心

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notify:) name:@"pp" object:nil];

    //第三个参数:这里专门等着接的通知名

    //第一个和第二个参数:截到对应通知名的通知后,用第一个参数去执行第二个参数的方法

    // Do any additional setup after loading the view.

}


-(void)notify:(NSNotification*)noti

{

    UIColor *color = [noti object];//发通知的时候传过的参数,就是这里的这个noti,无论什么类型的参数,都用object去解析,解析完了,发的时候是什么类型,就是什么类型

    self.view.backgroundColor = color;

}


//touch事件,去第二个页面

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"松手了");

    SecondViewController *second = [[SecondViewController alloc] init];

    second.delegate = self;//AAAA 被触发者的第二件事儿

    [self.navigationController pushViewController:second animated:YES];

    [second release];

}

//AAAA 实现代理的方法

-(void)getColor:(UIColor *)color

{

    self.view.backgroundColor = color;

}


-(void)viewWillAppear:(BOOL)animated

{

    static int num = 0;

    if(num != 0)

    {

        [self single];

        [self userdefaults];

    }

    num++;

}


-(void)single//BBBB 单例

{

    SingleTon *single = [SingleTon shareInstance];//还是那个

    self.view.backgroundColor = single.color;

}


-(void)userdefaults//CCCC 缓存

{

    NSArray *colorArr = @[[UIColor redColor],[UIColor yellowColor],[UIColor blueColor]];//数组存三色

    

    int num = [[[NSUserDefaults standardUserDefaults] objectForKey:@"color"] intValue];

    

    self.view.backgroundColor = colorArr[num];

}


3、SecondViewController.h文件中的写法

#import <UIKit/UIKit.h>

AAAA 声明代理

@protocol SecondDelegate <NSObject>


-(void)getColor:(UIColor*)color;


@end


@interface SecondViewController : UIViewController


@property(nonatomic,assign)id<SecondDelegate>delegate;


@end


4、SecondViewController.m文件中的写法

#import "SecondViewController.h"

#import "SingleTon.h"


@interface SecondViewController ()


@end


@implementation SecondViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor greenColor];

    

    [self makeBtn];

    // Do any additional setup after loading the view.

}


-(void)makeBtn

{

    NSArray *arr = @[@"red",@"yellow",@"blue"];

    for(int i =0;i<arr.count;i++)

    {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        btn.frame = CGRectMake(0, 64+i*60, 320, 60);

        [btn setTitle:arr[i] forState:UIControlStateNormal];

        [btn addTarget:self action:@selector(btnDown:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];

        btn.tag = 1000+i;

    }

}


-(void)btnDown:(UIButton*)btn

{

    NSArray *colorArr = @[[UIColor redColor],[UIColor yellowColor],[UIColor blueColor]];//数组存三色

    //该传值了

    

    [self delegateMethod:colorArr[btn.tag - 1000]];  AAAA

    [self singleMethod:colorArr[btn.tag - 1000]]; BBBB

    [self userdefaultsMethod:btn.tag - 1000];  CCCC

    [self notificationMethod:colorArr[btn.tag - 1000]]; DDDD

    

    [self.navigationController popViewControllerAnimated:YES];

}


-(void)delegateMethod:(UIColor*)color//AAAA 代理

{

    [self.delegate getColor:color];

}


-(void)singleMethod:(UIColor*)color//BBBB 单例

{

    SingleTon *single = [SingleTon shareInstance];

    single.color = color;//给单例的属性赋值

}


-(void)userdefaultsMethod:(int)color//CCCC 缓存

{

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

    [user setObject:[NSString stringWithFormat:@"%d",color] forKey:@"color"];

    [user synchronize];

}


-(void)notificationMethod:(UIColor*)color//DDDD 通知

{

    //1、做一个通知中心

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    //2、做一个通知内容

    NSNotification *notify = [NSNotification notificationWithName:@"pp" object:color];//第一个参数:通知名,第二个参数:通知携带的参数

    //3、用通知中心把通知内容post出去,然后就不管了

    [center postNotification:notify];//中心调方法,内容是参数

}


5SingleTon.h单例写法 BBBB

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>


@interface SingleTon : NSObject


@property(nonatomic,retain)UIColor *color;//用来保存全局颜色的


+(id)shareInstance;


@end


6SingleTon.m单例写法 BBBB

#import "SingleTon.h"

static SingleTon *single = nil;


@implementation SingleTon


+(id)shareInstance

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        single = [SingleTon alloc];//这里只走一次

    });

    return single;

}


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值