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

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

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



属性传值

SetProrerty.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface SetProrerty : NSObject  
  4.   
  5. @property (nonatomic,copy)    NSString     *string;  
  6. @property (nonatomic,strong)  NSArray      *array;  
  7. @property (nonatomic,strong)  NSDictionary *dictionary;  
  8.   
  9. - (void)setProrerty;  
  10. @end  

SetProrerty.m

[objc]  view plain  copy
  1. #import "SetProrerty.h"  
  2.   
  3. @implementation SetProrerty  
  4. @synthesize string     = _string;  
  5. @synthesize array      = _array;  
  6. @synthesize dictionary = _dictionary;  
  7.   
  8. - (void)setProrerty  
  9. {  
  10.     self.string = @"string";  
  11.     self.array  = @[@"object_0",@"object_1"];  
  12.     self.dictionary = @{@"key_0":@"value_0",@"key_1":@"value_1"};  
  13. }  
  14. @end  

GetProrerty.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface GetProrerty : NSObject  
  4. - (void)getProperty;  
  5. @end  

GetProrerty.m

[objc]  view plain  copy
  1. #import "GetProrerty.h"  
  2. #import "SetProrerty.h"  
  3.   
  4. @implementation GetProrerty  
  5. - (void)getProperty  
  6. {  
  7.     SetProrerty *setPro = [[SetProrerty alloc]init];  
  8.     [setPro setProrerty];  
  9.       
  10.     NSLog(@"\n========>prorerty:\n%@;\n%@;\n%@;",setPro.string,setPro.array,setPro.dictionary);  
  11.   
  12. }  
  13. @end  

代理传值

SetDelegate.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @protocol TheDelegate;  
  4. @interface SetDelegate : NSObject  
  5.   
  6. @property (nonatomic,weak)id <TheDelegate>delegate;  
  7.   
  8. - (void)callBackDelegate;  
  9. @end  
  10.   
  11. //创建协议  
  12. @protocol TheDelegate <NSObject>  
  13. @required  
  14. @optional  
  15. - (void)setDelegateWithString:(NSString *)string;  
  16. - (void)setDelegateWithArray:(NSArray *)array;  
  17. - (void)setDelegateWithDictionary:(NSDictionary *)dictionary;  
  18. - (void)setDelegateWithString:(NSString *)string andArray:(NSArray *)array andDictionary:(NSDictionary *)dictionary;  
  19.   
  20. @end  

SetDelegate.m

[objc]  view plain  copy
  1. #import "SetDelegate.h"  
  2.   
  3. @implementation SetDelegate  
  4. @synthesize delegate = _delegate;  
  5.   
  6. - (void)callBackDelegate  
  7. {  
  8.     //监听代理方法是否被实现,若实现则执行该代理方法  
  9.     if ([self.delegate respondsToSelector:@selector(setDelegateWithString:)]) {  
  10.         [self.delegate setDelegateWithString:@"string"];  
  11.     }  
  12.     if ([self.delegate respondsToSelector:@selector(setDelegateWithArray:)]) {  
  13.         [self.delegate setDelegateWithArray:@[@"object_0",@"object_1"] ];  
  14.     }  
  15.     if ([self.delegate respondsToSelector:@selector(setDelegateWithDictionary:)]) {  
  16.         [self.delegate setDelegateWithDictionary:@{@"key_0":@"value_0",@"key_1":@"value_1"}];  
  17.     }  
  18.     if ([self.delegate respondsToSelector:@selector(setDelegateWithString:andArray:andDictionary:)]) {  
  19.         [self.delegate setDelegateWithString:@"string" andArray:@[@"object_0",@"object_1"] andDictionary:@{@"key_0":@"value_0",@"key_1":@"value_1"}];  
  20.     }  
  21. }  
  22. @end  

GetDelegate.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2. #import "SetDelegate.h"  
  3.   
  4. @interface GetDelegate : NSObject<TheDelegate>  
  5.   
  6. - (void)getDelegate;  
  7.   
  8. @end  

GetDelegate.m

[objc]  view plain  copy
  1. #import "GetDelegate.h"  
  2.   
  3. @implementation GetDelegate  
  4.   
  5. - (void)getDelegate  
  6. {  
  7.     SetDelegate *setDelegate = [[SetDelegate alloc]init];  
  8.     setDelegate.delegate = self;  
  9.     [setDelegate callBackDelegate];  
  10. }  
  11.   
  12. //代理方法(string)  
  13. - (void)setDelegateWithString:(NSString *)string  
  14. {  
  15.     NSLog(@"\n========>delegate:\n%@", string);  
  16. }  
  17.   
  18. //代理方法(array)  
  19. - (void)setDelegateWithArray:(NSArray *)array  
  20. {  
  21.     NSLog(@"\n========>delegate:\n%@", array);}  
  22.   
  23. //代理方法(dictionary)  
  24. - (void)setDelegateWithDictionary:(NSDictionary *)dictionary  
  25. {  
  26.     NSLog(@"\n========>delegate:\n%@", dictionary);  
  27. }  
  28.   
  29. //代理方法(string, array, dictionary)  
  30. - (void)setDelegateWithString:(NSString *)string andArray:(NSArray *)array andDictionary:(NSDictionary *)dictionary  
  31. {  
  32.     NSLog(@"\n========>delegate:\n%@\n%@\n%@", string, array, dictionary);  
  33. }  
  34.   
  35. @end  

Block传值

SetBlock.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface SetBlock : NSObject  
  4.   
  5. - (void)setBlockWithString:(void(^)(NSString *string))string;  
  6. - (void)setBlockWithArray:(void(^)(NSArray *array))array;  
  7. - (void)setBlockWithDictionary:(void(^)(NSDictionary *dictionary))dictionary;  
  8. - (void)setBlockWithAll:(void(^)(NSString *string,NSArray *array,NSDictionary *dictionary))all;  
  9.   
  10. @end  

SetBlock.m

[objc]  view plain  copy
  1. #import "SetBlock.h"  
  2.   
  3. @implementation SetBlock  
  4.   
  5. - (void)setBlockWithString:(void(^)(NSString *string))string  
  6. {  
  7.     NSString *str = @"string";  
  8.     if (string)  
  9.     {  
  10.         string(str);  
  11.     }  
  12. }  
  13.   
  14. - (void)setBlockWithArray:(void(^)(NSArray *array))array  
  15. {  
  16.     NSArray *arr = @[@"object_0",@"object_1"];  
  17.     if (array)  
  18.     {  
  19.         array(arr);  
  20.     }  
  21. }  
  22. - (void)setBlockWithDictionary:(void(^)(NSDictionary *dictionary))dictionary  
  23. {  
  24.     NSDictionary *dic = @{@"key_0":@"value_0",@"key_1":@"value_1"};  
  25.     if (dictionary)  
  26.     {  
  27.         dictionary(dic);  
  28.     }  
  29. }  
  30.   
  31. - (void)setBlockWithAll:(void(^)(NSString *string,NSArray *array,NSDictionary *dictionary))all  
  32. {  
  33.     NSString *str = @"string";  
  34.     NSArray *arr = @[@"object_0",@"object_1"];  
  35.     NSDictionary *dic = @{@"key_0":@"value_0",@"key_1":@"value_1"};  
  36.       
  37.     if (all)  
  38.     {  
  39.         all(str,arr,dic);  
  40.     }  
  41.   
  42. }  
  43.   
  44. @end  

GetBlock.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. typedef void (^BlockWithString)(NSString *string);  
  4. typedef void (^BlockWithArray)(NSArray *array);  
  5. typedef void (^BlockWithDictionary)(NSDictionary *dictionary);  
  6. typedef void (^BlockWithAll)(NSString *string,NSArray *array,NSDictionary *dictionary);  
  7.   
  8. @interface GetBlock : NSObject  
  9.   
  10. @property (nonatomic,strong) BlockWithString stringBlock;  
  11. @property (nonatomic,strong) BlockWithArray arrayBlock;  
  12. @property (nonatomic,strong) BlockWithDictionary dictionaryBlock;  
  13. @property (nonatomic,strong) BlockWithAll allBlock;  
  14.   
  15. - (void)getBlock;  
  16. @end  

GetBlock.m

[objc]  view plain  copy
  1. #import "GetBlock.h"  
  2. #import "SetBlock.h"  
  3.   
  4. @implementation GetBlock  
  5.   
  6. @synthesize stringBlock     = _stringBlock;  
  7. @synthesize arrayBlock      = _arrayBlock;  
  8. @synthesize dictionaryBlock = _dictionaryBlock;  
  9. @synthesize allBlock        = _allBlock;  
  10.   
  11. - (void)getBlock  
  12. {  
  13.     SetBlock *block = [[SetBlock alloc]init];  
  14.       
  15.     [block setBlockWithString:^(NSString *string)  
  16.     {  
  17.         NSLog(@"============>block\n:%@",string);  
  18.     }];  
  19.       
  20.     [block setBlockWithArray:^(NSArray *array) {  
  21.         NSLog(@"============>block\n:%@", array);  
  22.     }];  
  23.       
  24.     [block setBlockWithDictionary:^(NSDictionary *dictionary) {  
  25.         NSLog(@"============>block\n:%@", dictionary);  
  26.     }];  
  27.       
  28.     [block setBlockWithAll:^(NSString *string, NSArray *array, NSDictionary *dictionary) {  
  29.         NSLog(@"============>block\n:%@\n%@\n%@", string, array, dictionary);  
  30.     }];  
  31. }  
  32. @end  

通知传值

SetNotification.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface SetNotification : NSObject  
  4.   
  5. - (void)setNotification;  
  6.   
  7. @end  

SetNotification.m

[objc]  view plain  copy
  1. #import "SetNotification.h"  
  2.   
  3. @implementation SetNotification  
  4.   
  5. - (void)setNotification  
  6. {  
  7.     //创建通知中心  
  8.     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];  
  9.       
  10.     //发送通知 以下是三种发送方式  //name:通知的名称  object:发通知的内容(只读)  userInfo:补充信息(字典)  
  11. //    [center postNotification:<#(NSNotification *)#>]  
  12. //    [center postNotificationName:<#(NSString *)#> object:<#(id)#>]  
  13. //    [center postNotificationName:<#(NSString *)#> object:<#(id)#> userInfo:<#(NSDictionary *)#>]  
  14.       
  15.     NSString *string = @"string";  
  16.     [center postNotificationName:@"string" object:string];  
  17.   
  18.     NSArray *array = @[@"object_1",@"object_1"];  
  19.     [center postNotificationName:@"array" object:array];  
  20.       
  21.     NSDictionary *dictionary = @{@"kye_0":@"value_0",@"key_1":@"value_1"};  
  22.     [center postNotificationName:@"dictionary" object:dictionary userInfo:nil];  
  23.       
  24.     //创建一个通知  
  25.     NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:string,@"string",array,@"array",dictionary,@"dictionary", nil nil];  
  26.     NSNotification *notification = [[NSNotification alloc]initWithName:@"all" object:string userInfo:dic];  
  27.     [center postNotification:notification];  
  28.       
  29. }  
  30.   
  31. @end  

GetNotification.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface GetNotification : NSObject  
  4.   
  5. - (void)getNotification;  
  6. @end  

GetNotification.m

[objc]  view plain  copy
  1. #import "GetNotification.h"  
  2. #import "SetNotification.h"  
  3.   
  4. @implementation GetNotification  
  5.   
  6. - (void)dealloc  
  7. {  
  8.     //注销通知 注册与注销要成对出现  
  9.     [[NSNotificationCenter defaultCenter]removeObserver:self];  
  10. }  
  11.   
  12. - (void)getNotification  
  13. {  
  14.       
  15.       
  16.     //接收方创建通知中心  
  17.     NSNotificationCenter * center = [NSNotificationCenter defaultCenter];  
  18.     //注册 Observer:注册者  name:要接收的通知的名称 ;  
  19.     //selector:接收到通知之后对应的操作方法;方法中传递的参数是接收到的通知  
  20.     //object:对发送者的要求;一般填nil,不关心是谁发的通知; 如果写 腾讯 ,意思是只能接收到腾讯发的通知;  
  21.     [center addObserver:self selector:@selector(receiveString:) name:@"string" object:nil];  
  22.     [center addObserver:self selector:@selector(receiveArray:) name:@"array" object:nil];  
  23.     [center addObserver:self selector:@selector(receiveDictionary:) name:@"dictionary" object:nil];  
  24.     [center addObserver:self selector:@selector(receiveAll:) name:@"all" object:nil];  
  25. }  
  26.   
  27. - (void)receiveString:(NSNotification*)notify  
  28. {  
  29.     id object = [notify object];  
  30.     NSLog(@"============>getNotification:\n%@ ",object);  
  31. }  
  32.   
  33. - (void)receiveArray:(NSNotification*)notify  
  34. {  
  35.     id object = [notify object];  
  36.     NSLog(@"============>getNotification:\n%@ ",object);  
  37. }  
  38.   
  39. - (void)receiveDictionary:(NSNotification*)notify  
  40. {  
  41.     id object = [notify object];  
  42.     NSLog(@"============>getNotification:\n%@ ",object);  
  43. }  
  44.   
  45.   
  46. - (void)receiveAll:(NSNotification*)notify  
  47. {  
  48.     //接收通知里传过来的信息  
  49.     NSDictionary *dic = [notify userInfo];  
  50.     NSLog(@"============>getNotification:\n%@",dic);  
  51.       
  52.       
  53. }  
  54.   
  55. @end  

本地存储传值

SetUserDefaults.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface SetUserDefaults : NSObject  
  4.   
  5. - (void)setUserDefaults;  
  6.   
  7. @end  

SetUserDefaults.m

[objc]  view plain  copy
  1. #import "SetUserDefaults.h"  
  2.   
  3. @implementation SetUserDefaults  
  4.   
  5. - (void)setUserDefaults  
  6. {  
  7.     [[NSUserDefaults standardUserDefaults] setObject:@"string" forKey:@"string"];  
  8.     [[NSUserDefaults standardUserDefaults] setObject:@[@"object_0",@"object_1"] forKey:@"array"];  
  9.     [[NSUserDefaults standardUserDefaults] setObject:@{@"key_0":@"value_0",@"key_1":@"value_1"} forKey:@"dictionary"];  
  10.       
  11.     //同步数据  
  12.     [[NSUserDefaults standardUserDefaults] synchronize];  
  13. }  
  14.   
  15. @end  

GetUserDefaults.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface GetUserDefaults : NSObject  
  4.   
  5. - (void)getUserDefaults;  
  6.   
  7. @end  

GetUserDefaults.m

[objc]  view plain  copy
  1. #import "GetUserDefaults.h"  
  2.   
  3. @implementation GetUserDefaults  
  4.   
  5. - (void)getUserDefaults  
  6. {  
  7.     NSLog(@"=============>NSUserDefaults:%@\n", [[NSUserDefaults standardUserDefaults] objectForKey:@"string"]);  
  8.     NSLog(@"=============>NSUserDefaults:%@\n", [[NSUserDefaults standardUserDefaults] objectForKey:@"array"]);  
  9.     NSLog(@"=============>NSUserDefaults:%@\n", [[NSUserDefaults standardUserDefaults] objectForKey:@"dictionary"]);  
  10.       
  11. //    //移除用户数据  
  12. //    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"string"];  
  13. //    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"array"];  
  14. //    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"dictionary"];  
  15.       
  16. }  

单例传值

SetSingleton.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface SetSingleton : NSObject  
  4.   
  5. - (void)setSingleton;  
  6.   
  7. @end  

SetSingleton.m

[objc]  view plain  copy
  1. #import "SetSingleton.h"  
  2. #import "Singleton.h"  
  3.   
  4. @implementation SetSingleton  
  5.   
  6. - (void)setSingleton  
  7. {  
  8. //    [[Singleton shareData]setString:@"string"];  
  9. //     跟上面的写法是等价的  
  10.     Singleton *single = [Singleton shareData];  
  11.     single.string     = @"string";  
  12.     single.array      = @[@"object_0",@"object_1"];  
  13.     single.dictionary = @{@"key_0":@"value_0",@"key_1":@"value_1"};  
  14. }  
  15.   
  16. @end  

GetSingleton.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface GetSingleton : NSObject  
  4.   
  5. - (void)getSingleton;  
  6.   
  7. @end  

GetSingleton.m

[objc]  view plain  copy
  1. #import "GetSingleton.h"  
  2. #import "Singleton.h"  
  3.   
  4. @implementation GetSingleton  
  5.   
  6. - (void)getSingleton  
  7. {  
  8.     Singleton *single = [Singleton shareData];  
  9.     NSLog(@"\n========>singleton:\n%@;\n%@;\n%@;",single.string,single.array,single.dictionary);  
  10. }  
  11.   
  12. @end  

Singleton.h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Singleton : NSObject  
  4.   
  5. @property (nonatomic,copy)    NSString     *string;  
  6. @property (nonatomic,strong)  NSArray      *array;  
  7. @property (nonatomic,strong)  NSDictionary *dictionary;  
  8.   
  9. + (Singleton *)shareData;  
  10. @end  
Singleton.m

[objc]  view plain  copy
  1. #import "Singleton.h"  
  2.   
  3. @implementation Singleton  
  4. @synthesize string     = _string;  
  5. @synthesize array      = _array;  
  6. @synthesize dictionary = _dictionary;  
  7.   
  8. static Singleton *singletonData = nil;  
  9.   
  10. + (Singleton *)shareData  
  11. {  
  12.     if (!singletonData)  
  13.     {  
  14.         singletonData = [[Singleton alloc]init];  
  15.     }  
  16.       
  17.     return singletonData;  
  18. }  
  19.   
  20. @end  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值