iOS传值方式总结
- 属性传值
前向后传值。
1.属性传值,传值第一步就得确定传的属性类型,然后就定义什么样的属性
2.属性传值,就是上一个界面到下一个界面的选中方法里面将要传的值传到 下一个界面
例子:
MainViewController与SecondViewContrller两个视图控制器,点击MainViewController中的按钮跳转到SecondViewcontroller视图,同时想传递一个值过去,用到属性传值。
代码例子:
首先在SecondViewcontrolle视图中需要定义一个属性,存储传递过来的值:
@property(nonatomic,retain)NSString *nameStr;
之后MainViewcontroller中方法事件,通过SecondViewcontroller的对象将需要传递的值,存储在nameStr中
-(void) name
{
SecondViewController *second = [[SecondViewController alloc] init];
Second.nameStr = @”pegboggs”;
[Self.navigationController pushViewController:second animated:YES];
}
- 方法传值
需求同一中的属性传值一样,区别在于使用方法传值,可以直接将方法与初始化方法合并,此时当触发MainViewController的点击事件并跳转时,直接通过初始化将值保存。
初始化方法如下:首先还是在SecondViewcontroller中定义一个属性,保存传过来的数值
@property(nonatomic,retain)NSString *nameStr;
//重写初始化方法,用于传值
-(id) initWithValue(NSString*)value
{
if(self = [super initWithNibName:nil bundle:nil])
{
self.nameStr = value;
}
return self;
}
方法传值:
-(void) name
{
SecondViewController *second = [[SecondViewController alloc] initWithValue:@”pegboggs”];
[self.navigationController
pushViewController:second animated:YES];
}
- 协议代理传值
这种传值主要用于A进入B,然后B输入值后传回给A,比较常见的就是用于修改个人信息,点击进入修改界面,修改完之后回到显示界面,显示修改后的结果(以前我都是用单例,虽然也可以实现,但是明显这种方法更加实用)。
在第二个界面里实现协议,并设置代理,第一个界面实现协议方法,并在跳转的时候,设置代理为本身,在第二个界面,修改数据的时候,执行代理方法,实现数据传递
代码例子:
第一个界面.h文件
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface ViewController : UIViewController<theValue>
@property (nonatomic,strong) UILabel *nameLab;
@property (nonatomic,strong) UILabel *ageLab;
@end
.m文件
#import "ViewController.h"
#import "FirstViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize nameLab,ageLab;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
[btn setTitle:@"跳转" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
nameLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, 100, 30)];
nameLab.text = @"name";
[self.view addSubview:nameLab];
ageLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 250, 100, 30)];
ageLab.text = @"age";
[self.view addSubview:ageLab];
}
-(void) btn
{
FirstViewController *first = [[FirstViewController alloc] init];
first.delegate = self;
[self presentViewController:first animated:YES completion:nil];
}
//协议实现
-(void) passValue:(NSString *)value
{
nameLab.text = value;
ageLab.text = value;
}
第二个界面.h文件
#import <UIKit/UIKit.h>
@protocol theValue <NSObject>
-(void)passValue:(NSString*)value;
@end
@interface FirstViewController : UIViewController
@property (nonatomic,strong) UITextField *nameField;
@property (nonatomic,strong) UITextField *ageField;
@property (nonatomic,assign) NSObject<theValue> *delegate;
@end
.m文件
#import "FirstViewController.h"
@interface FirstViewController ()<theValue>
@end
@implementation FirstViewController
@synthesize nameField,ageField;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 100, 30)];
[btn setTitle:@"dismis" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
nameField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 100, 30)];
nameField.placeholder = @"name";
[self.view addSubview:nameField];
ageField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 100, 30)];
ageField.placeholder = @"age";
[self.view addSubview:ageField];
}
-(void) btn
{
[self.delegate passValue:nameField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
- block传值
首先在第二界面的.h文件里定义声明block属性
typedef void (^ReturnTextBlock)(NSString *showText);
@property (nonatomic, copy) ReturnTextBlock returnTextBlock;
-(void) returnText :(ReturnTextBlock)block;
.m文件中实现方法
-(void) returnText:(ReturnTextBlock)block{
self.returnTextBlock
= block;
}
把传进来的Block语句块保存到本类的实例变量returnTextBlock
最后在第一个视图中,获得第二个视图的控制器,并且用第二个视图控制器来调用定义的属性
-(void) btn
{
FirstViewController *first = [[FirstViewController alloc] init];
// first.delegate = self;
[first returnText:^(NSString *showText) {
nameLab.text = showText;
}];
[self presentViewController:first animated:YES completion:nil];
}
- 单例传值
单例只会对某个类实例化一次/单例类,对单例这个类实例化一次有且仅有一个对象
你单例初始化,只能初始化一次,然后你指向的对象,其实都是指向一个内存地址,也就是同一块内存,所以都是一样的/
那么,只能有一个对象,就是实例化的那个
单例方法使用很简单,就是相当于定义了整个工程使用的变量,在整个工程中可以随时调用,随时更改,全局唯一。主要实现代码如下
.h文件(没有定义变量,需要直接定义即可)
@interface Single : NSObject
+(Single*) sharedInstance;
-(id)init;
@end
.m文件
#import "Single.h"
static Single *instance = nil;
@implementation Single
+(Single*) sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
instance = [[Single alloc] init];
});
return instance;
}
-(id) init
{
if (self = [super init]) {
}
return self;
}
-(id) copyWithZone:(NSZone*)zone
{
return instance;
}
+(id) allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken,^{
instance = [super allocWithZone:0];
});
return instance;
}
@end
- 通知传值
NSNotificationCenter提供了一种更加解耦的方式。最典型的应用就是任何对象对可以发送通知到中心,同时任何对象可以监听中心的通知。
发送通知的代码如下:
[[NSNotificationCenter
defaultCenter] postNotificationName:@”myNotificationName” object:broadcasterObject];
注册接收通知的代码如下:
[[NSNotificationCenter
defaultCenter] addObserver:listenerObject selector:@selector(receivingMethodOnListener:) name:@”myNotificationName” object:nil];
注册通知的时候可以指定一个具体的广播者对象,但这不是必须的。你可能注意到了defaultCenter
。实际上这是你在应用中会使用到的唯一的中心。通知会向整个应用开放,因此只有一个中心。
同时还有一个NSDistributedNotificationCenter。这是用来应用间通信的。在整个计算机上只有一个该类型的中心。
优点: 通知的发送者和接受者都不需要知道对方。可以指定接收通知的具体方法。通知名可以是任何字符串。
缺点: 较键值观察需要多点代码。在删掉前必须移除监听者。 不能传大量数值,只能让谁去做什么事。