IOS知识整理

iOS Block的传值 代理传值 通知中心传值

2016-08-17  本文已影响532人  iOS学末

有的时候,我们需要页面跳转传递数据,就需要将这个页面通过某种形式传递给另一个页面。我们把两个页面分别记做:传值页面 接受页面

某种形式传递包括:Block, 代理(delegate), 通知(Notification)

谁传值谁就设置代理 谁传值谁就设置Block

Block传值

1.使用Block属性实现回调传值
typedef void(^sendValue)(NSString *context);
@interface SecondViewController : UIViewController
@property (copy,nonatomic) sendValue sendValueBlock;
- (IBAction)sendButton:(UIButton *)sender 
{
    
    self.sendValueBlock(self.textFile.text);
   [self.navigationController popViewControllerAnimated:YES];
}
#import "FirstViewController.h"
@implementation FirstViewController
-(void)buttonAction:(UIButton *)button
{ 
   SecondViewController *secondVC = [[SecondViewController alloc]init]; 
   __weak FirstViewController *firstVC = self; 
   secondVC.sendValue = ^(NSString *str){ 
    firstVC.label .text = str; 
};
 [self.navigationController pushViewController:secondVC animated:YES];
}
2.方法中定义Block实现回调传值
typedef void(^sendValue)(NSString *context);
-(void)sendString:(NSString *)string andBlock: (sendValue)block;
-(void)sendString:(NSString *)string andBlock: (sendValue)block
{
   block(string);
}
SecondViewController *secondVC = [[SecondViewController alloc]init]; 
[secondVC sendString:(NSString *)string  andBlock:^(NSString*string) {
 self.label.text = string;
}
];
block传值两种方法 有问题欢迎指正

2.代理传值

//设置代理 协议名的命名规范:类名+delegate
@protocol sendValueDelegate <NSObject>
//传值的内容作为协议方法的参数
- (void)sendString:(NSString *)string;
@end
@interface SecondViewController : UIViewController
@property (weak,nonatomic) id<sendValueDelegate> delegate;
- (IBAction)backButton:(UIButton *)sender {
    //判断代理人是否存在,和是否实现了代理方法
    if (self.delegate && [self.delegate respondsToSelector:@selector(sendString:)]) {
        [self.delegate sendString:self.textFile.text];
    }
   [self dismissViewControllerAnimated:YES completion:nil];
}
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()<sendValueDelegate>
@end
#import "FirstViewController.h"
-(void)buttonAction:(UIButton *)button
{ 
  SecondViewController *secondVC = [[SecondViewController alloc]init]; 
  secondVC.delegate = self; 
  [self.navigationController pushViewController:secondVC animated:YES];
}
@end
#import "FirstViewController.h"
-(void)sendString:(NSString *)string
{ 
  self.label.text = string;
}
@end

通知中心传值

@property (readonly, copy) NSString *name;//这个成员变量是这个消息对象的唯一标识,用于辨别消息对象。
@property (readonly, retain) id object;//这个成员变量定义一个对象,可以理解为针对某一个对象的消息。
@property (readonly, copy) NSDictionary *userInfo;这个成员变量是一个字典,可以用其来进行传值。
示例代码

给接收值的页面注册一个消息通知注意:

#import "RootViewController.h"
@implementation RootViewController 
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotifi:) name:@"test" object:nil];
@end
receiveNotifi方法的实现
#import "RootViewController.h"
@implementation RootViewController
-(void)receiveNotifi:(NSNotification *)notifi
{    
    NSLog(@"object=====%@",notifi.object); 
    NSLog(@"userInfo=====%@",notifi.userInfo);
}
@end
第二步在要传值出去的界面发送通知消息
#import "SecondViewController.h"
@implementation SecondViewController
-(IBAction)button:(id)sender { 
[[NSNotificationCenter defaultCenter]postNotificationName:@"test" object:self.myTextField userInfo:@{@"title":self.myTextField.text}]; 
[self.navigationController popViewControllerAnimated:YES];
}
@end

文/Joker_King(简书作者)原文链接:http://www.jianshu.com/p/1b4d69e6cb4a著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

上一篇 下一篇

猜你喜欢

热点阅读