您的当前位置:首页正文

通知的简单使用

来源:华拓网
  • 模型数据

// XMGWine.h
#import <Foundation/Foundation.h>

@interface XMGWine : NSObject
@property (copy, nonatomic) NSString *money;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *image;

/** 购买的数量*/
@property (nonatomic ,assign) int count;
@end

// XMGWine.m
#import "XMGWine.h"

@implementation XMGWine


// XMGWineCell.h
#import <UIKit/UIKit.h>

@class XMGWine;
@interface XMGWineCell : UITableViewCell

/** 酒模型*/
@property (nonatomic ,strong) XMGWine *wine;

@end

// XMGWineCell.m

#import "XMGWineCell.h"
#import "XMGWine.h"
#import "XMGCircleButton.h"

@interface XMGWineCell ()
@property (weak, nonatomic) IBOutlet UIImageView *imageImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet XMGCircleButton *minusButton;

@end
@implementation XMGWineCell

- (void)setWine:(XMGWine *)wine
{
    _wine = wine;
    self.imageImageView.image = [UIImage imageNamed:wine.image];

    self.nameLabel.text = wine.name;

    self.moneyLabel.text = wine.money;

    // 根据count决定countLabel显示的文字
    self.countLabel.text = [NSString stringWithFormat:@"%d",wine.count];
    // 根据count决定减号是否能点击
    self.minusButton.enabled = (wine.count > 0);
}

/**
 * 加号点击
 */
- (IBAction)plusClick {
    // 修改模型
    self.wine.count ++ ;
    // 修改界面
    self.countLabel.text = [NSString stringWithFormat:@"%d",self.wine.count];
    // 减号按钮一定能点击
    self.minusButton.enabled = YES;

    // 发布通知
    /**
     *  postNotificationName :  通知的名称
     *  object :    通知的发布者
     */
    [[NSNotificationCenter defaultCenter] postNotificationName:@"plusButtonClickNotification" object:self];
}

/**
 *  减号点击
 */
- (IBAction)minusClick {
    // 修改模型
    self.wine.count -- ;
    // 修改界面
    self.countLabel.text = [NSString stringWithFormat:@"%d",self.wine.count];

    // 减号按钮不能点击
    if (self.wine.count == 0) {
        self.minusButton.enabled = NO;
    }

    // 发布通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"minusButtonClickNotification" object:self];
}
@end



// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

// ViewController.m


#import "ViewController.h"
#import "XMGWine.h"
#import "MJExtension.h"
#import "XMGWineCell.h"

@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 所有的酒数据*/
@property (nonatomic ,strong) NSArray *wineArray;

/** 总价*/
@property (weak, nonatomic) IBOutlet UILabel *totalPriceLabel;
@end

@implementation ViewController

#pragma mark - 懒加载
- (NSArray *)wineArray
{
    if (!_wineArray) {
        _wineArray = [XMGWine mj_objectArrayWithFilename:@"wine.plist"];
    }
    return _wineArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // 监听通知
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    /**
     *  @param addObserver: 谁接收这个通知
     *  @param selector: 调用什么方法
     *  @param name: 通知的名称
     *  @param object: 通知发布者
     */
    [center addObserver:self selector:@selector(plusClick:) name:@"plusButtonClickNotification" object:nil];
    [center addObserver:self selector:@selector(minusClick:) name:@"minusButtonClickNotification" object:nil];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 通知的监听方法
- (void)plusClick:(NSNotification *)note
{
    // 通知发布者
    XMGWineCell *cell = note.object;

    // 计算总价
    int totalPrice = self.totalPriceLabel.text.intValue + cell.wine.money.intValue;
    // 设置总价
    self.totalPriceLabel.text = [NSString stringWithFormat:@"%d",totalPrice];
}

- (void)minusClick:(NSNotification *)note
{
    // 通知发布者
    XMGWineCell *cell = note.object;

    // 计算总价
    int totalPrice = self.totalPriceLabel.text.intValue - cell.wine.money.intValue;
    // 设置总价
    self.totalPriceLabel.text = [NSString stringWithFormat:@"%d",totalPrice];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.wineArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 访问缓存池
    static NSString *ID = @"wine";
    XMGWineCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 设置数据
    cell.wine = self.wineArray[indexPath.row];
    return cell;
}
@end



    // 发出通知
    [LZNotificationCenter postNotificationName:LZUpdateLocationNotification object:nil userInfo:@{@"location" : userLocation}];

    // 添加监听事件
    - (void)setupNote
    {
        [LZNotificationCenter addObserver:self selector:@selector(updateLocation:) name:LZUpdateLocationNotification object:nil];
    }

    - (void)updateLocation:(NSNotification *)noti{

//    NSLog(@"%@", noti.userInfo[@"location"]);
    BMKUserLocation *userLocation = noti.userInfo[@"location"];

    NSLog(@"latitude is %f", userLocation.location.coordinate.latitude);
    NSLog(@"longitude is %f", userLocation.location.coordinate.longitude);

    NSString *str = [NSString stringWithFormat:@"%f,%f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude];
    NSData *data1 = [str dataUsingEncoding:NSUTF8StringEncoding];

    // sendData方法返回值是void,发送数据成功之后,
    [self.mConnection sendData:data1 toHost:@"192.168.2.10" port:18601 withTimeout:-1 tag:0];

    }

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