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

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

事例Demo:链接: 密码: 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;@endSetProrerty.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"};}@endGetProrerty.h#import <Foundation/Foundation.h>@interface GetProrerty : NSObject- (void)getProperty;@endGetProrerty.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;@endSetDelegate.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"}];}}@endGetDelegate.h#import <Foundation/Foundation.h>#import "SetDelegate.h"@interface GetDelegate : NSObject<TheDelegate>- (void)getDelegate;@endGetDelegate.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);}@endBlock传值

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;@endSetBlock.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);}}@endGetBlock.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;@endGetBlock.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通知传值旅行,重复一个承诺和梦想,听他第二十八次提起童年往事,

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

相关文章:

你感兴趣的文章:

标签云: