通知的简单使用

作者: Z了个L | 来源:发表于2016-07-28 14:45 被阅读36次
  • 模型数据

// 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];
    }

相关文章

  • 简单使用通知

    之前用通知老是不知道该在哪个界面定义通知,哪个界面调用通知,哪里移除通知。即使我当前知道了,但是没过多久我就又忘记...

  • 通知简单使用

    1.创建通知,通过通知中心发送通知(比如你在a控制器想让b控制器做点什么的时候,并且控制器没什么关系的情况下) N...

  • 通知的简单使用

    模型数据

  • 通知的简单使用

  • iOS通知的简单使用

    NSNotificationCenter NSNotification是iOS中的一个调度消息通知的类,采用单利模...

  • Android 通知的简单使用

    如何创建通知 随着Android系统不断升级,Notification的创建方式也随之变化,主要变化如下: And...

  • 通知Notification的简单使用

    均属于笔记,仅供个人参考,有问题欢迎指正,整理模式 一,通知的基本应用 通知的用法还是比较灵活的,既可以在活动里创...

  • iOS 通知的简单使用

    1.注册通知+发送通知 2.接受通知 3.实现方法

  • Android通知栏显示通知简单使用

    最近做直播,要求向关注者发通知,显示在通知栏, 记录下简单的使用。 并发现一个在魅族手机上奇葩的坑。。。 直接上代...

  • Android Notificatin 通知

    通知的简单使用 1-通知渠道:android 8.0 必须设置通知渠道,否则通知不会显示。2-通知图标问题:and...

网友评论

    本文标题:通知的简单使用

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