3:通知传值
通知中心
NSNotificationCenter提供了一种更加解耦的方式。最典型的应用就是任何对象对可以发送通知到中心,同时任何对象可以监听中心的通知。
发送通知的代码如下:
[[NSNotificationCenter defaultCenter] postNotificationName:@”myNotificationName” object:broadcasterObject];
注册接收通知的代码如下:
[[NSNotificationCenter defaultCenter] addObserver:listenerObject selector:@selector(receivingMethodOnListener:) name:@”myNotificationName” object:nil];
注册通知的时候可以指定一个具体的广播者对象,但这不是必须的。你可能注意到了defaultCenter 。实际上这是你在应用中会使用到的唯一的中心。通知会向整个应用开放,因此只有一个中心。
同时还有一个NSDistributedNotificationCenter。这是用来应用间通信的。在整个计算机上只有一个该类型的中心。
优点: 通知的发送者和接受者都不需要知道对方。可以指定接收通知的具体方法。通知名可以是任何字符串。
缺点: 较键值观察需要多点代码。在删掉前必须移除监听者。 不能传大量数值,只能让谁去做什么事。
4:代理传值(Delegate)
其中有两个ViewController分别对应两个界面,一个协议PassValueDelegate用来实现传值协议,UserEntity是传递数据的对象。
以下是实现的效果:点击Open进入Second界面,输入完毕点击OK后回到First界面并显示结果
协议中声明的方法:
copy
#import
@ class UserEntity;
@protocol PassValueDelegate
-( void )passValue:(UserEntity *)value;
@end
在第一个窗口实现协议:
#import
#import "PassValueDelegate.h"
//第一个窗口遵守PassValueDelegate
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *nameLabel;
@property (retain, nonatomic) IBOutlet UILabel *ageLabel;
@property (retain, nonatomic) IBOutlet UILabel *gendarLabel;
- (IBAction)openBtnClicked:(id)sender;
@end
.m文件中实现协议的方法:
[cpp]view plaincopy
//实现协议,在第一个窗口显示在第二个窗口输入的值方法
-( void )passValue:(UserEntity *)value
{
self.nameLabel.text = value.userName;
self.ageLabel.text = [NSString stringWithFormat:@ "%d" ,value.age];
self.gendarLabel.text = value.gendar;
}
点击Open按钮所触发的事件:
[cpp]view plaincopy
//点击进入第二个窗口的方法
- (IBAction)openBtnClicked:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@ "SecondViewController" bundle:[NSBundle mainBundle]];
//设置第二个窗口中的delegate为第一个窗口的self
secondView.delegate = self;
[self.navigationController pushViewController:secondView animated:YES];
}
第二个窗口中声明一个NSObject对象,该对象遵守PassValueDelegate协议:
[cpp]view plaincopy
#import
#import "PassValueDelegate.h"
@interface SecondViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *nameTextField;
@property (retain, nonatomic) IBOutlet UITextField *ageTextFiled;
@property (retain, nonatomic) IBOutlet UITextField *gendarTextField;
//这里用assign而不用retain是为了防止引起循环引用。
@property(nonatomic,assign) NSObject *delegate;
- (IBAction)okBtnClicked:(id)sender;
- (IBAction)closeKeyboard:(id)sender;
@end
输入完毕后,点击OK按钮所触发的事件:
[cpp]view plaincopy
- (IBAction)okBtnClicked:(id)sender {
UserEntity *userEntity = [[UserEntity alloc] init];
userEntity.userName = self.nameTextField.text;
userEntity.gendar = self.gendarTextField.text;
userEntity.age = [self.ageTextFiled.text intValue];
//通过委托协议传值
[self.delegate passValue:userEntity];
//退回到第一个窗口
[self.navigationController popViewControllerAnimated:YES];
[userEntity release];
}
以上就实现了使用Delegate在两个ViewController之间传值,这种场景一般应用在进入子界面输入信息,完后要把输入的信息回传给前一个界面的情况,比如修改用户个人信息,点击修改进入修改界面,修改完后到显示界面显示修改后的结果。
5:Block传值
1.第一页中 声明一个 block, 需要传入一个颜色 , 让当前的 view 变色
// 声明一个 block, 需要传入一个颜色 , 让当前的 view 变色
void (^changeColor)( UIColor *color) = ^( UIColor *color){
self . view . backgroundColor = color;
};
2 . 第一页中 //block 传值 --------- 将 block 给第二个页面
SecondViewController *secondVC = [[ SecondViewController alloc ] init ];
//block 传值 --------- 将 block 给第二个页面
secondVC. block = changeColor;
3.第二页中定义 -- 当 block 变量作为一个类的属性 , 必须要使用 copy 修饰
//block 传值 --------- 将 block 给第二个页面
//block 传值 --- 当 block 变量作为一个类的属性 , 必须要使用 copy 修饰
@property ( nonatomic , copy ) void (^block)( UIColor *color);
4.在第二页中给block传值
//block 传值 --------- 将传值给 block
NSArray *array = [ NSArray arrayWithObjects :[ UIColor yellowColor ], [ UIColor cyanColor ], [ UIColor greenColor ], [ UIColor brownColor ], nil ];
self . block ([array objectAtIndex : rand () % 4 ]);
网友评论