iOS 六种基本的传值方式

1、属性传值

利:正向传值、简单单一

弊:不能反向传值,也不能 跨页面传值

NextViewController* next = [[NextViewControlleralloc]initWithNibName:@"NextViewController"bundle:nil];

//1、属性传值---正向传值

next.str=@"属性传值";

2、单例传值

利:正向/方向传值,跨页面,数据写入内存,从内存读取

弊:需要单例类,并需要创建单例对象

[DefaultInstanceshareInstance].text=@"单例传值”;

DefaultInstance* discrip = [DefaultInstanceshareInstance];

// 2、单例传值  取值

self.textField.text= [DefaultInstanceshareInstance].text;

3、NSUserDefaults传值

利:正向/方向传值,跨页面,数据写入沙盒文件、然后去取

弊:

[[NSUserDefaultsstandardUserDefaults]setObject:@"NSUserDefaults传值"forKey:@"NSUserDefaults”];

self.textField.text= [[NSUserDefaultsstandardUserDefaults]objectForKey:@"NSUserDefaults"];

4、代理传值

利:经典传值,反向传值

弊:1对1 的传值,需要建立代理关系,写法复杂

next.delegate=self;

//代理传值--实现协议方法--接受来自页面2的值

- (void)passValue:(NSString*)str{

self.label.text= str;

}

@protocolPassValueDelegate

- (void) passValue:(NSString* )str;

@end

@property(nonatomic,weak)id delegate;//委托方持有协议,weak防止循环引用

if([_delegateconformsToProtocol:@protocol(PassValueDelegate)] && [_delegaterespondsToSelector:@selector(passValue:)]){

[self.delegatepassValue:_textField.text];

}

5、Block传值

利:苹果主推的传值方式,写法简单,可以实现所有代理的功能。

弊:1对1 的传值,反向传值

@property(nonatomic,copy)void(^block)(NSString* str);

self.block(_textField.text);

next.block= ^(NSString*str) {

self.label.text= str;

};

6、通知传值

利:多对多传值、跨页面,nil为群发

弊:类似有  收音机广播 :收音机必须打开

、、、

[[NSNotificationCenterdefaultCenter]postNotificationName:@"notify"object:niluserInfo:@{@"not":self.textField.text}];

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

- (void)nothandle:(NSNotification*)not{

self.label.text= not.userInfo[@"not"];

}

、、、

```

    _analysisConentStr= analysisConentStr;

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:analysisConentStr];;

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];

    [paragraphStylesetLineSpacing:10];

    [attributedStringaddAttribute:NSParagraphStyleAttributeNamevalue:paragraphStylerange:NSMakeRange(0, analysisConentStr.length)];

    self.contentLab.attributedText= attributedString;```

总结:(1、2、3)可以正向传值,(2、3、4、5)可以反向传值

```

/**

 * Set the imageView `image` with an `url` and optionally a placeholder image.

 *

 * The download is asynchronous and cached.

 *

 *@paramurl            The url for the image.

 *@paramplaceholder    The image to be set initially, until the image request finishes.

 *@paramoptions        The options to use when downloading the image.@seeSDWebImageOptions for the possible values.

 *@paramoperationKey  A string to be used as the operation key. If nil, will use the class name

 *@paramsetImageBlock  Block used for custom set image code

 *@paramprogressBlock  A block called while image is downloading

 *                      @notethe progress block is executed on a background queue

 *@paramcompletedBlock A block called when operation has been completed. This block has no return value

 *                      and takes the requested UIImage as first parameter. In case of error the image parameter

 *                      is nil and the second parameter may contain an NSError. The third parameter is a Boolean

 *                      indicating if the image was retrieved from the local cache or from the network.

 *                      The fourth parameter is the original image url.

 *@paramcontext        A context with extra information to perform specify changes or processes.

 */

- (void)sd_internalSetImageWithURL:(nullableNSURL*)url

                  placeholderImage:(nullableUIImage*)placeholder

                           options:(SDWebImageOptions)options

                      operationKey:(nullableNSString*)operationKey

                     setImageBlock:(nullableSDSetImageBlock)setImageBlock

                          progress:(nullableSDWebImageDownloaderProgressBlock)progressBlock

                         completed:(nullableSDExternalCompletionBlock)completedBlock

                           context:(nullableNSDictionaryid> *)context;

```

你可能感兴趣的:(iOS 六种基本的传值方式)