通知/代理/block 传值

作者: 小码码 | 来源:发表于2017-03-28 16:55 被阅读56次

1 通知的使用

适合没有关联的对象之间的传值和一对多情况的传值

1.通知发送者发布通知
 [[NSNotificationCenter defaultCenter] postNotificationName:@"plusClickNotification" object:self];

[[NSNotificationCenter defaultCenter] postNotificationName:@"minusClickNotification" object:self];

2.通知观察者注册并接收通知
// 监听通知
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(plusClick:) name:@"plusClickNotification" object:nil];    
    [center addObserver:self selector:@selector(minusClick:) name:@"minusClickNotification" object:nil];

- (void)dealloc{
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 通知的监听方法
- (void)plusClick:(NSNotification *)note
{
    // 通知的发布者
    WineCell *cell = note.object;
    // 计算总价
    int totalPrice = self.totalPriceLabel.text.intValue + cell.wine.money.intValue;
    // 设置总价
    self.totalPriceLabel.text = [NSString stringWithFormat:@"%d",totalPrice];
}

- (void)minusClick:(NSNotification *)note
{
    // 通知的发布者
    WineCell *cell = note.object;
    // 计算总价
    int totalPrice = self.totalPriceLabel.text.intValue - cell.wine.money.intValue;
    // 设置总价
    self.totalPriceLabel.text = [NSString stringWithFormat:@"%d",totalPrice];
}

2 代理

适合一对一情况的、并且有关联的对象之间的传值,代码略微分散

1.定义代理协议和代理属性
// 定义代理协议
@protocol WineCellDelegate <NSObject>
@optional
- (void)wineCellDidClickPlusBtn:(WineCell *)wineCell;

- (void)wineCellDidClickMinusBtn:(WineCell *)wineCell;
@end
// 代理属性
@property (nonatomic, weak) id<WineCellDelegate> delegate;

2.调用代理方法时先做判断
// 通知代理,调用代理方法
    // 判断能否调用代理方法
    if ([self.delegate respondsToSelector:@selector(wineCellDidClickPlusBtn:)]) {
        // 调用代理方法
        [self.delegate wineCellDidClickPlusBtn:self];
    }

 if([self.delegate respondsToSelector:@selector(wineCellDidClickMinusBtn:)]){
        [self.delegate wineCellDidClickMinusBtn:self];
    }

3.遵守代理协议,成为代理,实现代理方法
// 遵守代理协议
@interface ViewController ()<WineCellDelegate>

// 成为代理
cell.delegate = self;

#pragma mark - 实现代理方法
- (void)wineCellDidClickPlusBtn:(WineCell *)wineCell {

// 计算总价
    int totalPrice = self.totalPriceLable.text.intValue + wineCell.wineItem.money.intValue;
   
    // 设置总价
    self.totalPriceLable.text = [NSString stringWithFormat:@"%d",totalPrice];
    
    self.buyBtn.enabled = YES;
    
    if (![self.shopsArray containsObject:wineCell.wineItem]) {
        [self.shopsArray addObject:wineCell.wineItem];
    }
}

-(void)wineCellDidClickMinusBtn:(WineCell *)wineCell {

    // 计算总价
    int totalPrice = self.totalPriceLable.text.intValue - wineCell.wineItem.money.intValue;
    
    // 设置总价
    self.totalPriceLable.text = [NSString stringWithFormat:@"%d",totalPrice];
    
    self.buyBtn.enabled = totalPrice > 0;
    
    // 移除用户不再购买的商品
    if (wineCell.wineItem.count == 0) {
        [self.shopsArray removeObject:wineCell.wineItem];
    }
}

3 block传值

适合一对一情况的、并且有关联的对象之间的传值,代码比较集中。
参考文章:http://www.jianshu.com/p/7d729fc351c8

传值方:

//.h 文件

/**
 *  类型自定义
 */
typedef void (^ReturnValueBlock) (NSString *strValue);

@interface NextViewController : UIViewController
/**
 *  声明一个ReturnValueBlock属性,这个Block是获取传值的界面传进来的
 */
@property(nonatomic, copy) ReturnValueBlock returnValueBlock;

@end

=================================================================

//.m 文件

#import "NextViewController.h"

@interface NextViewController ()

@property (weak, nonatomic) IBOutlet UITextField *inputText;

- (IBAction)back:(id)sender;

@end

@implementation NextViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.title = @"第二个界面";
}


/**
 *  返回上一个界面
 *
 *  @param sender 按钮
 */
- (IBAction)back:(id)sender {

    NSString *inputString = self.inputText.text;

    if (self.returnValueBlock) {
        //将自己的值传出去,完成传值
        self.returnValueBlock(inputString);
    }

    [self.navigationController popViewControllerAnimated:YES];

}

@end

捕获方:

//.m 文件

#import "ViewController.h"
#import "NextViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *nextPassedValue;

- (IBAction)next:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

}

//点击按钮跳转到第二个界面
- (IBAction)next:(id)sender {

    NextViewController *nvc = [[NextViewController alloc]init];

    //赋值Block,并将捕获的值赋值给UILabel
    nvc.returnValueBlock = ^(NSString *passedValue){

        self.nextPassedValue.text = passedValue;

    };

    [self.navigationController pushViewController:nvc animated:YES];

}
@end

传值方式可参考的文章:
http://www.jianshu.com/p/51153323c8bf

相关文章

网友评论

    本文标题:通知/代理/block 传值

    本文链接:https://www.haomeiwen.com/subject/ovnpyttx.html