Men的博客

欢迎光临!

0%

界面传值

正向传值

1.什么是正向传值?
总结:A界面创建B界面
A界面的值传递到B界面
以后很多是有用到正向传值,只要在下一个界面的@proprty中添加一个属性。
实例:
登陆界面login 主界面main
把登陆界面上的用户名传到主界面
1.main上添加@proprty username
为什么定义@property属性
为了接受从登陆界面传过来的用户名。
@property (nonatomic ,copy)NSString *username;
2.传值。
MainViewController *mvc=[[MainViewController alloc]init];
把当前界面输入框中用户名传递给主界面。
注意:切换界面之前传值,千万不要放在present的后面。
mvc.username=_textField.text;
[self presentViewController:mvc animated:YES completion:nil];
3.第二个界面使用 self.object 使用值
nameLabel.text = [NSString stringWithFormat:@”用户名是: %@”,self.username];

反向传值

简单分解为四部分
1.制定协议
2.给主界面发送消息
3.主界面执行消息
4.配置界面的
1。先制定协议
为什么要制定协议
制定协议,协议中规定了配置界面应该给主界面发送什么消息
问题:谁制定协议呢?谁遵守协议呢?
配置界面制定协议,主界面遵守协议,实现协议中的方法
2.如何在配置界面添加主界面的指针
因为配置界面没有主界面的指针,所以,要把主界面的指针传到配置界面
给配置界面添加属性——传递主界面的指针
但是这个传入的对象必须要遵守协议,
代理一般使用assgin
3.如何传值
发送消息给配置界面让主界面去做事情
self,delegate代表主界面,self是配置界面。、

总结:
A界面创建B界面,
B把值(字体大小)传回给A界面
注意:使用到 协议,代理,比较复杂。
搞清楚问题。
主界面创建配置界面。
配置界面要修改主界面
但是配置界面没有主界面的指针。
MainViewController
ConfigViewController

(1)制定协议
因为是ConfigViewController其他界面发送消息

  1. 告诉其他界面我要给你发送什么消息
  2. 确保其他实现了消息对应的方法

@protocol ConfigViewControllerDelegate
//遵守协议的其他类必须实现的方法
-(void)changeFontSize:(int)size;
@end

@interface ConfigViewController : UIViewController

//作用: 保存主界面的指针
//为什么保存, 最后给主界面发送消息修改字体
//细节1: assgin 确保不会循环引用
//细节2: id 如果是id, 能传入任意界面了
//细节3: 遵守协议意味着可以任意对象, 但是这个对象必须遵守协议
@property (assign,nonatomic) id delegate;

@end

//int size = fontTextField.text.intValue
[self.delegate changeFontSize:fontTextField.text.intValue];
//切换到配置界面
ConfigViewController *cvc = [[ConfigViewController alloc] init];

//作用: 把当前界面的指针传到cvc中
//为啥: 希望配置界面中字体改变通知主界面
cvc.delegate = self;

[self presentViewController:cvc animated:YES completion:nil];

@interface MainViewController ()

//改变字体的方法
-(void)changeFontSize:(int)size
{
novelLabel.font = [UIFont systemFontOfSize:size];
}

单例传值

需求: A–>B–>C–>D–>E–>F
需要A中得数据在F界面上显示出来
解决: 使用单例传值解决
如何实现一个单例类?
实例: 在AppDelegate中存储作者和版本
在配置界面上显示出来
@interface DataCenter : NSObject
//表示获取一个共享的实例
+(id)sharedInstance;
@property (copy,nonatomic) NSString appInfo;
@end
@implementation DataCenter
表示获取一个共享的实例
+(id)sharedInstance
{
//关键
//第一次执行: dc为空, 会申请一个对象
//以后执行: dc不为空, 直接返回以前创建的对象
static DataCenter
dc= nil;
if(dc == nil)
{
//dc = [[DataCenter alloc] init];
dc = [[[self class] alloc] init];
}
return dc;
}
设置单利对象
DataCenter *dc=[DataCenter sharedInstance];
dc.author=@”quiet”;
先获取单利对象
DataCenter *dc=[DataCenter sharedInstance];
UILabel *authorLibel=[[UILabel alloc]initWithFrame:CGRectMake(100, 50, 100, 30)];
authorLibel.text=dc.author;
要求非常高单例类往往重写
alloc,dealloc,retain,release,
autorelease,copyWithZone

通知传值

需求: 1对n通信, 配置要换肤, 其他所有界面响应
发出换肤的通知
获取系统通知类的单例对象
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
发出一个通知
参数Name: 通知的名字
参数object: 要发给那个对象, nil表示不限制对象
参数userInfo: 通知附加的信息, 皮肤颜色穿过去
[nc postNotificationName:@”changeSkin” object:nil userInfo:@{@”skinColor”:@”yellow”}];
为了获取换肤的通知
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(dealChangeSkin:) name:@”changeSkin” object:nil];
}
注意: 参数类型不是NSNotificationCenter
-(void)dealChangeSkin:(NSNotification *)notification
{
获取通知中附加的信息
NSString *skinColor = notification.userInfo[@”skinColor”];
NSLog(@”c = %@”,skinColor);
if([skinColor isEqualToString:@”yellow”])
{
self.view.backgroundColor = [UIColor yellowColor];
}
}

block传值