1.正向传值 — 属性传值/方法传值
SecondViewController *secondVC = [[SecondViewController alloc]init];
NSString *str = @"hello";
2.反向传值
1.代理
//主动方 要传递值的一方
//1.声明协议
@protocol giveDataDelegate <NSObject>
//2.拟定一个方法 被动方需要什么 参数就是什么
- (void)giveDataFrom:(NSString *)str andColor:(UIColor *)color;
@end
@interface NextViewController : UIViewController
//3.声明一个持有协议的id指针 属性修饰符weak/assign
@property (nonatomic,weak) id<giveDataDelegate>delegate;
//传递数值
[self.delegate giveDataFrom:@"I AM BACK" andColor:[UIColor yellowColor]];
//接收数据方
//1.遵守协议
@interface ViewController ()<giveDataDelegate>
//2.实现协议方法
- (void)giveDataFrom:(NSString *)str andColor:(UIColor *)color{
//主动方要求被动方要做的事情
…
}
//3.设置代理
nextVC.delegate = self;
2.block
//主动方 要传递值的一方
//typedef 定义一个新的类型 名字ColorBlock
typedef void(^ColorBlock)(NSString *,UIColor *);
@property (nonatomic,copy) ColorBlock block;
//接收数据方
NextViewController *nextVC = [[NextViewController alloc]init];
nextVC.block = ^(NSString *str , UIColor *color){
//主动方让被动方做的事情
//数据回调回来之后 需要处理的事情
…
};
3.使用单例传值
+(SingleClass *)defaultSingle{
//首先需要一个静态变量 初始值是nil
static SingleClass *obj = nil;//可以拿到上一次的值 //当第二次....第n次调用这个方法 obj值就是第一次创建的对象的指针xxxxx000000 静态变量相当于全局变量 程序退出时静态变量才会消失 也可以放在外面
//判断
if (obj == nil) {
///如果为空 创建对象
obj = [[SingleClass alloc]init];
}
return obj;//xxxxx000000
}
4.通知中心
//发送数据方
//通知中心 只有一个 --- 单例对象
//拿到通知中心
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//发布通知(发送消息)
[center postNotificationName:@"频道1" object:nil userInfo:@{@"label":@"爸爸回来了"}];
//[center postNotificationName:@"频道1" object:@"爸爸去哪了"];
//要传递的数据设置成字典的value
//接收数据方
//拿到通知中心
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//注册监听者(观察者)
[center addObserver:self selector:@selector(changeLabel:) name:@"频道1" object:nil];//object通常是nil
//Observer 通过 name(频道) 接收数据 接收成功调用selector
- (void)changeLabel:(NSNotification *)noti{
NSLog(@"第二个界面:名字 %@ 发布者 %@ 数据 %@",noti.name,noti.object,noti.userInfo);
UILabel *label = (id)[self.view viewWithTag:200];
label.text = noti.userInfo[@"label"];
}
//移除掉所有监听器
[[NSNotificationCenter defaultCenter] removeObserver:self];
实例:键盘收放
—系统自动发布通知
//注册 监听键盘弹起的时候
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//注册 监听键盘落下的时候
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
//键盘将要弹起
- (void)keyboardWillShow:(NSNotification *)noti{
//拿到键盘弹起的时间 时间是秒
NSTimeInterval interval = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// NSNumber of double
//拿到键盘弹起后的Rect
NSValue *value = noti.userInfo[UIKeyboardFrameEndUserInfoKey];
//NSValue of CGRect
//非对象类型用getValue来提取
CGRect rect = [value CGRectValue];
[UIView animateWithDuration:interval animations:^{
UIView *redView = (id)[self.view viewWithTag:100];
CGRect redRect = redView.frame;
//屏幕高度 - 键盘高度 - 自身高度
redRect.origin.y = [UIScreen mainScreen].bounds.size.height - rect.size.height - redRect.size.height;
redView.frame = redRect;
}];
}
- 单例对象
uiwindows
// AppDelegate 也是一个单例
// NSFileManager //文件管理
// NSNotificationCenter //通知中心
// UIApplication
// NSUserDefaults //应用程序设置
// NSURLCache //url缓存
// NSHTTPCookieStorage 应用程序cookies池