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

来源:互联网 发布:mac 思维导图 免费 编辑:程序博客网 时间:2024/06/04 08:55

一、传值简介


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 = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];

    self.window.backgroundColor = [UIColorwhiteColor];

    

    FirstViewController *first = [[FirstViewControlleralloc] init];

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

    [first release];

    self.window.rootViewController = nav;

    [nav release];

    

    [self.windowmakeKeyAndVisible];

    return YES;

}


2FirstViewController.m文件中的写法

#import "FirstViewController.h"

#import "SecondViewController.h"

#import "SingleTon.h"


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


@end


@implementation FirstViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    

    self.view.backgroundColor = [UIColorredColor];

    //DDDD 通知中心

    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(notify:)name:@"pp"object:nil];

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

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

    // Do any additional setup after loading the view.

}


-(void)notify:(NSNotification*)noti

{

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

    self.view.backgroundColor = color;

}


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

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

{

    NSLog(@"松手了");

    SecondViewController *second = [[SecondViewControlleralloc] init];

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

    [self.navigationControllerpushViewController: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 = [SingleTonshareInstance];//还是那个

    self.view.backgroundColor = single.color;

}


-(void)userdefaults//CCCC缓存

{

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

    

    int num = [[[NSUserDefaultsstandardUserDefaults] 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 {

    [superviewDidLoad];

    

    self.view.backgroundColor = [UIColorgreenColor];

    

    [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 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

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

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

        [self.viewaddSubview: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.navigationControllerpopViewControllerAnimated:YES];

}


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

{

    [self.delegategetColor:color];

}


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

{

    SingleTon *single = [SingleTonshareInstance];

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

}


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

{

    NSUserDefaults *user = [NSUserDefaultsstandardUserDefaults];

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

    [user synchronize];

}


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

{

    //1、做一个通知中心

    NSNotificationCenter *center = [NSNotificationCenterdefaultCenter];

    //2、做一个通知内容

    NSNotification *notify = [NSNotificationnotificationWithName:@"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 = [SingleTonalloc];//这里只走一次

    });

    return single;

}


@end



0 0
原创粉丝点击