iOS 几种传值方式(属性,代理,block,通知,本地存储,单例类)附Demo

本来这次的代码写的比较简单,除了传值方式的选项外,没添加别的控件,只是把传过去的值在控制台打印了一下,但由于把多种传值方式放在了同一篇里面,所以文章会显得过长,文章内容都是直接粘的代码,主要是为了方便查看逻辑,有不清楚的地方,朋友们可以直接下载Demo看具体的结构和代码。

事例Demo:链接: http://pan.baidu.com/s/1pJmlr9D 密码: 7faq



属性传值

SetProrerty.h

#import <Foundation/Foundation.h>

@interface SetProrerty : NSObject

@property (nonatomic,copy)    NSString     *string;
@property (nonatomic,strong)  NSArray      *array;
@property (nonatomic,strong)  NSDictionary *dictionary;

- (void)setProrerty;
@end

SetProrerty.m

#import "SetProrerty.h"

@implementation SetProrerty
@synthesize string     = _string;
@synthesize array      = _array;
@synthesize dictionary = _dictionary;

- (void)setProrerty
{
    self.string = @"string";
    self.array  = @[@"object_0",@"object_1"];
    self.dictionary = @{@"key_0":@"value_0",@"key_1":@"value_1"};
}
@end

GetProrerty.h

#import <Foundation/Foundation.h>

@interface GetProrerty : NSObject
- (void)getProperty;
@end

GetProrerty.m

#import "GetProrerty.h"
#import "SetProrerty.h"

@implementation GetProrerty
- (void)getProperty
{
    SetProrerty *setPro = [[SetProrerty alloc]init];
    [setPro setProrerty];
    
    NSLog(@"\n========>prorerty:\n%@;\n%@;\n%@;",setPro.string,setPro.array,setPro.dictionary);

}
@end

代理传值

SetDelegate.h

#import <Foundation/Foundation.h>

@protocol TheDelegate;
@interface SetDelegate : NSObject

@property (nonatomic,weak)id <TheDelegate>delegate;

- (void)callBackDelegate;
@end

//创建协议
@protocol TheDelegate <NSObject>
@required
@optional
- (void)setDelegateWithString:(NSString *)string;
- (void)setDelegateWithArray:(NSArray *)array;
- (void)setDelegateWithDictionary:(NSDictionary *)dictionary;
- (void)setDelegateWithString:(NSString *)string andArray:(NSArray *)array andDictionary:(NSDictionary *)dictionary;

@end

SetDelegate.m

#import "SetDelegate.h"

@implementation SetDelegate
@synthesize delegate = _delegate;

- (void)callBackDelegate
{
    //监听代理方法是否被实现,若实现则执行该代理方法
    if ([self.delegate respondsToSelector:@selector(setDelegateWithString:)]) {
        [self.delegate setDelegateWithString:@"string"];
    }
    if ([self.delegate respondsToSelector:@selector(setDelegateWithArray:)]) {
        [self.delegate setDelegateWithArray:@[@"object_0",@"object_1"] ];
    }
    if ([self.delegate respondsToSelector:@selector(setDelegateWithDictionary:)]) {
        [self.delegate setDelegateWithDictionary:@{@"key_0":@"value_0",@"key_1":@"value_1"}];
    }
    if ([self.delegate respondsToSelector:@selector(setDelegateWithString:andArray:andDictionary:)]) {
        [self.delegate setDelegateWithString:@"string" andArray:@[@"object_0",@"object_1"] andDictionary:@{@"key_0":@"value_0",@"key_1":@"value_1"}];
    }
}
@end

GetDelegate.h

#import <Foundation/Foundation.h>
#import "SetDelegate.h"

@interface GetDelegate : NSObject<TheDelegate>

- (void)getDelegate;

@end

GetDelegate.m

#import "GetDelegate.h"

@implementation GetDelegate

- (void)getDelegate
{
    SetDelegate *setDelegate = [[SetDelegate alloc]init];
    setDelegate.delegate = self;
    [setDelegate callBackDelegate];
}

//代理方法(string)
- (void)setDelegateWithString:(NSString *)string
{
    NSLog(@"\n========>delegate:\n%@", string);
}

//代理方法(array)
- (void)setDelegateWithArray:(NSArray *)array
{
    NSLog(@"\n========>delegate:\n%@", array);}

//代理方法(dictionary)
- (void)setDelegateWithDictionary:(NSDictionary *)dictionary
{
    NSLog(@"\n========>delegate:\n%@", dictionary);
}

//代理方法(string, array, dictionary)
- (void)setDelegateWithString:(NSString *)string andArray:(NSArray *)array andDictionary:(NSDictionary *)dictionary
{
    NSLog(@"\n========>delegate:\n%@\n%@\n%@", string, array, dictionary);
}

@end

Block传值

SetBlock.h

#import <Foundation/Foundation.h>

@interface SetBlock : NSObject

- (void)setBlockWithString:(void(^)(NSString *string))string;
- (void)setBlockWithArray:(void(^)(NSArray *array))array;
- (void)setBlockWithDictionary:(void(^)(NSDictionary *dictionary))dictionary;
- (void)setBlockWithAll:(void(^)(NSString *string,NSArray *array,NSDictionary *dictionary))all;

@end

SetBlock.m

#import "SetBlock.h"

@implementation SetBlock

- (void)setBlockWithString:(void(^)(NSString *string))string
{
    NSString *str = @"string";
    if (string)
    {
        string(str);
    }
}

- (void)setBlockWithArray:(void(^)(NSArray *array))array
{
    NSArray *arr = @[@"object_0",@"object_1"];
    if (array)
    {
        array(arr);
    }
}
- (void)setBlockWithDictionary:(void(^)(NSDictionary *dictionary))dictionary
{
    NSDictionary *dic = @{@"key_0":@"value_0",@"key_1":@"value_1"};
    if (dictionary)
    {
        dictionary(dic);
    }
}

- (void)setBlockWithAll:(void(^)(NSString *string,NSArray *array,NSDictionary *dictionary))all
{
    NSString *str = @"string";
    NSArray *arr = @[@"object_0",@"object_1"];
    NSDictionary *dic = @{@"key_0":@"value_0",@"key_1":@"value_1"};
    
    if (all)
    {
        all(str,arr,dic);
    }

}

@end

GetBlock.h

#import <Foundation/Foundation.h>

typedef void (^BlockWithString)(NSString *string);
typedef void (^BlockWithArray)(NSArray *array);
typedef void (^BlockWithDictionary)(NSDictionary *dictionary);
typedef void (^BlockWithAll)(NSString *string,NSArray *array,NSDictionary *dictionary);

@interface GetBlock : NSObject

@property (nonatomic,strong) BlockWithString stringBlock;
@property (nonatomic,strong) BlockWithArray arrayBlock;
@property (nonatomic,strong) BlockWithDictionary dictionaryBlock;
@property (nonatomic,strong) BlockWithAll allBlock;

- (void)getBlock;
@end

GetBlock.m

#import "GetBlock.h"
#import "SetBlock.h"

@implementation GetBlock

@synthesize stringBlock     = _stringBlock;
@synthesize arrayBlock      = _arrayBlock;
@synthesize dictionaryBlock = _dictionaryBlock;
@synthesize allBlock        = _allBlock;

- (void)getBlock
{
    SetBlock *block = [[SetBlock alloc]init];
    
    [block setBlockWithString:^(NSString *string)
    {
        NSLog(@"============>block\n:%@",string);
    }];
    
    [block setBlockWithArray:^(NSArray *array) {
        NSLog(@"============>block\n:%@", array);
    }];
    
    [block setBlockWithDictionary:^(NSDictionary *dictionary) {
        NSLog(@"============>block\n:%@", dictionary);
    }];
    
    [block setBlockWithAll:^(NSString *string, NSArray *array, NSDictionary *dictionary) {
        NSLog(@"============>block\n:%@\n%@\n%@", string, array, dictionary);
    }];
}
@end

通知传值

SetNotification.h

#import <Foundation/Foundation.h>

@interface SetNotification : NSObject

- (void)setNotification;

@end

SetNotification.m

#import "SetNotification.h"

@implementation SetNotification

- (void)setNotification
{
    //创建通知中心
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    //发送通知 以下是三种发送方式  //name:通知的名称  object:发通知的内容(只读)  userInfo:补充信息(字典)
//    [center postNotification:<#(NSNotification *)#>]
//    [center postNotificationName:<#(NSString *)#> object:<#(id)#>]
//    [center postNotificationName:<#(NSString *)#> object:<#(id)#> userInfo:<#(NSDictionary *)#>]
    
    NSString *string = @"string";
    [center postNotificationName:@"string" object:string];

    NSArray *array = @[@"object_1",@"object_1"];
    [center postNotificationName:@"array" object:array];
    
    NSDictionary *dictionary = @{@"kye_0":@"value_0",@"key_1":@"value_1"};
    [center postNotificationName:@"dictionary" object:dictionary userInfo:nil];
    
    //创建一个通知
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:string,@"string",array,@"array",dictionary,@"dictionary", nil];
    NSNotification *notification = [[NSNotification alloc]initWithName:@"all" object:string userInfo:dic];
    [center postNotification:notification];
    
}

@end


GetNotification.h

#import <Foundation/Foundation.h>

@interface GetNotification : NSObject

- (void)getNotification;
@end

GetNotification.m

#import "GetNotification.h"
#import "SetNotification.h"

@implementation GetNotification

- (void)dealloc
{
    //注销通知 注册与注销要成对出现
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)getNotification
{
    
    
    //接收方创建通知中心
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //注册 Observer:注册者  name:要接收的通知的名称 ;
    //selector:接收到通知之后对应的操作方法;方法中传递的参数是接收到的通知
    //object:对发送者的要求;一般填nil,不关心是谁发的通知; 如果写 腾讯 ,意思是只能接收到腾讯发的通知;
    [center addObserver:self selector:@selector(receiveString:) name:@"string" object:nil];
    [center addObserver:self selector:@selector(receiveArray:) name:@"array" object:nil];
    [center addObserver:self selector:@selector(receiveDictionary:) name:@"dictionary" object:nil];
    [center addObserver:self selector:@selector(receiveAll:) name:@"all" object:nil];
}

- (void)receiveString:(NSNotification*)notify
{
    id object = [notify object];
    NSLog(@"============>getNotification:\n%@ ",object);
}

- (void)receiveArray:(NSNotification*)notify
{
    id object = [notify object];
    NSLog(@"============>getNotification:\n%@ ",object);
}

- (void)receiveDictionary:(NSNotification*)notify
{
    id object = [notify object];
    NSLog(@"============>getNotification:\n%@ ",object);
}


- (void)receiveAll:(NSNotification*)notify
{
    //接收通知里传过来的信息
    NSDictionary *dic = [notify userInfo];
    NSLog(@"============>getNotification:\n%@",dic);
    
    
}

@end

本地存储传值

SetUserDefaults.h

#import <Foundation/Foundation.h>

@interface SetUserDefaults : NSObject

- (void)setUserDefaults;

@end

SetUserDefaults.m

#import "SetUserDefaults.h"

@implementation SetUserDefaults

- (void)setUserDefaults
{
    [[NSUserDefaults standardUserDefaults] setObject:@"string" forKey:@"string"];
    [[NSUserDefaults standardUserDefaults] setObject:@[@"object_0",@"object_1"] forKey:@"array"];
    [[NSUserDefaults standardUserDefaults] setObject:@{@"key_0":@"value_0",@"key_1":@"value_1"} forKey:@"dictionary"];
    
    //同步数据
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end

GetUserDefaults.h

#import <Foundation/Foundation.h>

@interface GetUserDefaults : NSObject

- (void)getUserDefaults;

@end

GetUserDefaults.m

#import "GetUserDefaults.h"

@implementation GetUserDefaults

- (void)getUserDefaults
{
    NSLog(@"=============>NSUserDefaults:%@\n", [[NSUserDefaults standardUserDefaults] objectForKey:@"string"]);
    NSLog(@"=============>NSUserDefaults:%@\n", [[NSUserDefaults standardUserDefaults] objectForKey:@"array"]);
    NSLog(@"=============>NSUserDefaults:%@\n", [[NSUserDefaults standardUserDefaults] objectForKey:@"dictionary"]);
    
//    //移除用户数据
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"string"];
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"array"];
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"dictionary"];
    
}

单例传值

SetSingleton.h

#import <Foundation/Foundation.h>

@interface SetSingleton : NSObject

- (void)setSingleton;

@end

SetSingleton.m

#import "SetSingleton.h"
#import "Singleton.h"

@implementation SetSingleton

- (void)setSingleton
{
//    [[Singleton shareData]setString:@"string"];
//     跟上面的写法是等价的
    Singleton *single = [Singleton shareData];
    single.string     = @"string";
    single.array      = @[@"object_0",@"object_1"];
    single.dictionary = @{@"key_0":@"value_0",@"key_1":@"value_1"};
}

@end

GetSingleton.h

#import <Foundation/Foundation.h>

@interface GetSingleton : NSObject

- (void)getSingleton;

@end

GetSingleton.m

#import "GetSingleton.h"
#import "Singleton.h"

@implementation GetSingleton

- (void)getSingleton
{
    Singleton *single = [Singleton shareData];
    NSLog(@"\n========>singleton:\n%@;\n%@;\n%@;",single.string,single.array,single.dictionary);
}

@end

Singleton.h

#import <Foundation/Foundation.h>

@interface Singleton : NSObject

@property (nonatomic,copy)    NSString     *string;
@property (nonatomic,strong)  NSArray      *array;
@property (nonatomic,strong)  NSDictionary *dictionary;

+ (Singleton *)shareData;
@end
Singleton.m

#import "Singleton.h"

@implementation Singleton
@synthesize string     = _string;
@synthesize array      = _array;
@synthesize dictionary = _dictionary;

static Singleton *singletonData = nil;

+ (Singleton *)shareData
{
    if (!singletonData)
    {
        singletonData = [[Singleton alloc]init];
    }
    
    return singletonData;
}

@end


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值