一.属性传值:
一般用于上级页面传值到下级页面,属性声明在下级页面,用于接收上级传过来的值.
secondViewController跳转到detailViewController并且,title的值赋给textfield
SecondViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
DetailViewController * detailVC = (DetailViewController*)segue.destinationViewController;//推出页面
detailVC .text = self.navigationItem.title;//属性传值
}
DetailViewController.h
@property(nonatomic,retain)NSString * text;//接受上级页面传过来的字符串
DetailViewController.m
self.textField.text = _text;//赋值
二、单例传值
单例传值类似与属性传值,不过相对麻烦,充当小三角色
和应用程序生命周期一致,仅有一个
创建单例类SingleInstance
SingleInstance.h
@interface SingleInstance : NSObject
+ (instancetype)shareInstance;//创建类方法
@property(nonatomic,retain)NSString * text;//保存值的
@end
SingleInstance.m
static SingleInstance * instance = nil;
//方法一, 主线程
//+(instancetype)shareInstance
//{
// @synchronized(self){//同步锁
// if (instance == nil ) {
// instance = [[SingleInstance alloc]init ];
// }
// }
//
// return instance;
//}
//GCD方式,block放在队列中,
+ (instancetype)shareInstance
{ static dispatch_once_t predicate;
dispatch_once(&predicate, ^{//检查参数是不是执行一次
instance = [[SingleInstance alloc ]init ];
});
return instance ;
}
SecondViewController.m
SingleInstance * instance = [SingleInstance shareInstance];
instance.text = self.navigationItem.title;
DetailViewController.m
_textField.text = instance.text;
三.代理传值:
实质:通过协议方法中的<参数>实现值传递:如- (void)sendValue:(NSString *)text
口诀:①.哪个页面(类)需要值,这个页面(类)即代理,协议方法的实现和遵守协议以及设置代理就写在哪个页面(类)里.
②.声明代理属性,就写在传值的那个页面(类)里,这个页面(类)即委托人.
③.想什么时候将值传过去,那就是让[代理 执行协议方法:实参值];
//注意:如果想用好代理-协议:
①.要明确谁是委托人:registerVC
②.谁是代理:loginVC
③.委托人要让代理做什么事:RegisterViewControllerDelegate
其实:
registerVC只是负责将值传到上级页面(代理),代理接收到值,至于接收到的值,代理loginVC怎么处理,registerVC不管.
设置协议及方法
//SecondViewController.h
@protocol secondViewDelegate
-(void)showName:(NSString *)nameString;
@end
设置代理(为防止循环引用,此次采用了weak)
//SecondViewController.h
@interface SecondViewController : UIViewController
@property (nonatomic, weak)id<secondViewDelegate> delegate;
@property (nonatomic, copy) ablock block;
@end
点击按钮传递数组让其显示
//SecondViewController.m
- (IBAction)delegateMethod:(id)sender {
if ([self notEmpty]) {
[self.delegate showName:self.nameTextField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}else{
[self showAlert];
}
}
调用,显示
//RootViewController.m
-(void)showName:(NSString *)nameString{
self.nameLabel.text = nameString;
}
四、Notification传值
favoriteViewController.m
//向通知中心发送响应
//1.获取通知中心对象
NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
//2.第三个参数表示传到通知中心的值
NSDictionary * value = @{@"cowName":@"小黄",@"color":[UIColor yellowColor]};
[center postNotificationName:@"findCow" object:self userInfo: value ];
DetailViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//通知中心()
//获取通知中心这个单例对象
NSNotificationCenter * notificationCenter = [NSNotificationCenter defaultCenter];
//2.注册通知
//第一个参数:表示给通知中心添加一个观察者。第二个参数:表示当对这个通知有人响应的时候,观察者要执行的方法,
//第三个参数:表示这个通知名。第四个参数:表示响应者(向通知中心发送响应的人),如果是nil,表示任何人都可以响应通知
[notificationCenter addObserver:self selector:@selector(payMoney:) name:@"findCow" object:nil];
}
- (void)payMoney:(NSNotification * )notification
{
//还钱
NSLog(@"name = %@",notification.name);
NSLog(@"object = %@",notification.object);
NSLog(@"userInfo = %@",notification.userInfo);
UIColor * color = notification.userInfo[@"color"];
self.view.backgroundColor = color;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"findCow" object:nil];
[super dealloc];
}