属性传值,协议传值,block传值,单例传值四种界面传值方式
一、属性传值
对于属性传值而言,相对于其它的三种 方法来说,是最基础,最简单的一种 方法,但,属性传值 有很大的局限性,因为是适用于第一个界面向第二个界面传 值,第二个向第三个界面传值等等。N界面向N + 1界面传值。而在此基础上,必须知道跳转界面的明确位置及所要传的值的具体类型。在第二个界面中声明所要传值 类型的属性。
@interface SecondViewController : UIViewController //声明一个字符串属性来保存第一个界面传过来的字符串内容
@property (nonatomic, copy)NSString *string;
@property (nonatomic, copy)UIColor *color;
@property (nonatomic, retain)UILabel *label;
当然,在第二个界面中使用所声明的属性
- (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = _color;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(, , , );
[button setTitle:self.string forState:UIControlStateNormal];
在属性传值 的时候,需要知道明确跳转的位置及所要传的值的类型。
@implementation FirstViewController - (void)right{
SecondViewController *secondVC = [[SecondViewController alloc] init];
secondVC.string = self.textField.text;
secondVC.color = self.view.backgroundColor;
[self.navigationController pushViewController:secondVC animated:YES];
}
二、协议传值
通常,协议传值可以和属性传值一起使用,属性传值是从前往后依次传值,而协议 是从后往前来传值。
协议传值可以简单的分为六步来实现。
1.声明协议 2.声明代理 3.执行协议方法 4.接收协议 5.设置协议 代理对象 6.实现协议方法
//1.声明协议
@protocol FourViewControllerDelegate <NSObject>
- (void)change:(NSString *)string;
- (void)changeColor:(UIColor *)color; @end @interface FourViewController : UIViewController @property (nonatomic, retain)UITextField *textField;
//2.声明代理
@property (nonatomic, assign)id<FourViewControllerDelegate>fouDelegate; @end
@implementation FourViewController - (void)back{
//3.执行协议方法
if (self.fouDelegate != nil && [self.fouDelegate respondsToSelector:@selector(change:)]) {
[self.fouDelegate change:self.textField.text];
[self.fouDelegate changeColor:self.view.backgroundColor];
} [self.navigationController popViewControllerAnimated:YES]; }
而如果想要使用协议,而必须要接收协议
//5.接收协议
@interface ThreeViewController : UIViewController<FourViewControllerDelegate> @property (nonatomic, retain)UILabel *label; @end
@implementation ThreeViewController //6.实现协议方法
- (void)change:(NSString *)string{
self.label.text = string;
}
- (void)changeColor:(UIColor *)color{
self.view.backgroundColor = color;
}
-(void)button{
FourViewController *fourVC = [[FourViewController alloc] init];
//4.指定第二个界面的代理对象为第一个视图控制器
fourVC.fouDelegate = self;
[self.navigationController pushViewController:fourVC animated:YES];
}
三、block传值
相比于前两种来说,应该知道,block是匿名函数。既然是函数,也就会有那四种 表现形式,无参无返回值,有参无返回值,有参无返回值,有参有返回值这四种 形式。所以 block相对于前两种来说,表现方式 多样。
__block int a = ;
void (^block1)(void) = ^(void){
a ++;
NSLog(@"%d",a);
};
block1(); void (^block2)(int age, NSString *string) = ^(int age, NSString *string){
NSLog(@"age is %d, text is %@", age, string);
};
block2(, @"xiaoming"); NSString *(^block3)(void) = ^(void){
return @"有参wu返回值";
};
NSLog(@"block3 is %@", block3());
NSString *(^block4)(NSString *text) = ^(NSString *string){
return [string stringByAppendingString:@"有返回值"];
};
NSLog(@"block4 is %@",block4(@"有参有返回值"));
block和协议传值的作用类似,但不同于协议传值的繁琐,block有很好的操作性。
在第二个界面声明block
#import <UIKit/UIKit.h> typedef void (^BaDaBlock)(NSString *); typedef void (^colorBlock)(UIColor *); @interface SecondViewController : UIViewController @property (nonatomic, retain)UITextField *textField; @property (nonatomic, copy)BaDaBlock bada;
@property (nonatomic, copy)colorBlock cBlock; @end
Block声明成属性,一定要用copy
因为Block存储的内容在栈区,用copy复制一份到堆区,不能用retain的原因是,retain只能使堆区中的引用计数加1,而不能使栈区的引用计数加1
在第二个界面执行所声明的blcok方法
@implementation SecondViewController
- (void)back{
//执行Block if (self.bada != nil ) {
self.bada(self.textField.text);
} if (self.cBlock != nil) {
self.cBlock(self.view.backgroundColor);
}
[self.navigationController popViewControllerAnimated:YES];
}
Block专门释放的方法 Block_release(_bada);
Block_release(_cBlock);
使用在第一个界面使用block
@implementation FirstViewController - (void)button{
SecondViewController *secondVC = [[SecondViewController alloc] init];
secondVC.bada = ^(NSString *str){
self.label.text = str;
};
secondVC.cBlock = ^(UIColor *color){
self.view.backgroundColor = color;
};
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
四、单例传值
单例,简单的来说,就是单个的实例对象,不管被创建多少次,都具有唯一性,只有惟一的一个。
单例的使用范围不限定,但需要知道明确跳转的位置,和所需传值 的类型。
而单例 ,系统内部也有定义,像UIScreen, UIDevice都是系统内部定义 的单例,而通常使用的则是用户自定义的单例
#import <Foundation/Foundation.h> @interface Handler : NSObject @property (nonatomic, copy)NSString *string; //单例方法是类方法,返回值类型为instancetype
//自己定义的单例类,方法名通常都以share开头
+(instancetype)shareInstance; @end
实现所声明的单例方法
#import "Handler.h" static Handler *handler = nil;
@implementation Handler +(instancetype)shareInstance{
//加锁,保证创建过程在同一时间内只能允许一个线程对象访问
@synchronized(self){
if (nil == handler) {
handler = [[Handler alloc]init];
}
}
return handler;
} @end
在实现的声明的单例方法时,一定要确保所创建的单例是唯一的。
在实现跳转的地方创建单例,将值赋给单例类声明的属性。
- (void)getAction{
TwoViewController *twoVC = [[TwoViewController alloc] init]; //创建单例
Handler *handler = [Handler shareInstance];
handler.string = _textField.text; [self.navigationController pushViewController:twoVC animated:YES];
}
使用传过来的单例值
@implementation ThreeViewController - (void)viewDidLoad {
[super viewDidLoad]; Handler *handler = [Handler shareInstance];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
label.text = handler.string;
注意:
因为单例可以全局使用,非常方便,并且唯一性。那怎么不大量使用呢?当然不能大量使用,因为是全局可以使用,相当于static一样,一旦创建,将不能被系统进行回收,所以 这块内存区域会一直存在,直到程序退出,如果大量采用单例的话,会造成大量的内存空间浪费。可以 使用,但不能大量的使用。
属性传值,协议传值,block传值,单例传值四种界面传值方式的更多相关文章
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
第二个视图控制器如何获取第一个视图控制器的部分信息 例如 :第二个界面中的lable显示第一个界面textField中的文本 这就需要用到属性传值.block传值 那么第一个视图控制器如何获的第二个视 ...
- iOS传值方式:属性,代理,block,单例,通知
正向传值均可,反向传值除属性传值不可,其余均可.下面简单介绍: (一)属性传值 第二个界面中的lable显示第一个界面textField中的文本 首先我们建立一个RootViewControllers ...
- swift实现单例的四种方式
单例模式 单例模式是设计模式中最简单的一种,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象. 当你只需要一个实例的时候需要使用单例 ...
- iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...
- iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNot ...
- 页面之间传值方式的总结,五种方式,通知,block,代理,单例,NSUERDEFALUT,
首先代码拿上 1:单例 2:通知 3:代理 4:block方法 5:NSUSERDEFAULT(沙盒文件) 先是单例类: .h文件 @interface DataSource : NSObject @ ...
- 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错
原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...
- iOS单例的两种实现
单例模式算是开发中比较常见的一种模式了.在iOS中,单例有两种实现方式(至少我目前只发现两种).根据线程安全的实现来区分,一种是使用@synchronized,另一种是使用GCD的dispatch_o ...
- java单例的几种实现方法
java单例的几种实现方法: 方式1: public class Something { private Something() {} private static class LazyHolder ...
随机推荐
- Qt Creator提示"Qt没有被正确安装,请运行make install"的解决办法
笔者最近使用Qt在开发一些小程序,觉得这个框架设计确实很好,使用了信号和槽解决了组件之间的通讯问题,可以说是基于C++语言上一个非常大的创新,大大提高了开发人员的编码效率,也使整个C++语言更加抽象. ...
- 域环境下装SQL SERVER的一次惨痛经历
SQL SERVER 2008 R2 其实sql server不建议装在域环境下的,但sharepoint必须用域用户来连接.这本来也不是个什么大问题,但是,这一次相当的不顺利哦. 我有单独的域控,单 ...
- GO RPC
HTTP RPC SERVER CODE package main import ( "errors" "fmt" "net/http" & ...
- decimalFormat(小数格式)
这个格式是用来形容小数的,所以只对小数部分起作用 0 一个数字 # 一个数字,不包括 0 (0和#就是一个占位符,有几个就意味着要显示多少位,区别是0 匹配任意数字,#匹配不包括0的任意数字(最后的0 ...
- 根据 url请求数据
public static JSONObject getJsonFromUrl(String url){ CloseableHttpClient httpClient = HttpClients.cr ...
- Oracle 10g提权测试
一直想摸索一下orcl提权的方式,今天测试了一下10g,可以成功提权. C:\wmpub>sqlplus scott/tiger@orcl SQL*Plus: Release 10.2.0.1. ...
- AJAX部分---php-jquery-ajax;
AJAX的应用场景 1.异步搜索过滤内容数据 2.表单异步验证 3.异步加载页面“更多”数据 4.异步处理登录 5.异步处理用户名是否注册 AJAX的主要特点 1.在不刷新页面的情况下,与服务器进行异 ...
- dashboard
http://www.htmleaf.com/pins/chart-doc/index.html http://www.flotcharts.org/flot/examples/ https://gi ...
- C# 中Join( )的理解
在MSDN中对Join( )的解释比较模糊:在继续执行标准的 COM 和 SendMessage 消息泵处理期间,阻塞调用线程(线程A),直到某个线程终(线程B)止为止. 首先来看一下有关的概念: 我 ...
- QString转换为char* (转)
Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...