程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 通知的使用

通知的使用

編輯:C++入門知識

通知的使用


Main.m

#import "Children.h"
#import "Nurse.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        
        Children *children = [[Children alloc] init];
        
        Nurse *nurse = [[Nurse alloc] initWithChildren:children];
        
        [[NSRunLoop currentRunLoop] run];
        
        [children release];
        [nurse release];
        
    }
    return 0;
}

Children.h

@interface Children : NSObject

@property(nonatomic,assign)NSInteger happyValue;    //歡樂值
@property(nonatomic,assign)NSInteger hungryValue;   //饑餓值

Children.m

- (id)init {

    self = [super init];
    
    if (self) {
        //開啟定時器
        [NSTimer scheduledTimerWithTimeInterval:1
                                         target:self
                                       selector:@selector(timeAction:)
                                       userInfo:nil
                                        repeats:YES];
        _hungryValue = 100;
        _happyValue = 100;
    }
    
    return self;
}

- (void)timeAction:(NSTimer *)time {

    --_hungryValue;
    --_happyValue;
    
    NSLog(@"happyValue:%ld",_happyValue);
    
    if (_happyValue < 90) {
        //通知保姆
        //發送一個通知,通知名:happlyValueNotification
        [[NSNotificationCenter defaultCenter] postNotificationName:@"happlyValueNotification" object:self];
        
    }
    
    NSLog(@"hungryValue:%ld",_hungryValue);
    
    if (_hungryValue < 85) {
        //發送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"hungryValueNotification" object:self];
    }
    
}

Nurse.h

@class Children;

@interface Nurse : NSObject {

    Children *_children;
    
}

- (id)initWithChildren:(Children *)children;

Nurse.m

#import "Children.h"

@implementation Nurse

- (id)initWithChildren:(Children *)children {

    self = [super init];
    
    
    if (self) {
        _children = [children retain];
        
        //接收通知,通知名:happlyValueNotification
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(happlyValueChange)
                                                     name:@"happlyValueNotification"
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hungryValueChange:) name:@"hungryValueNotification" object:nil];
        
    }
 
    return self;
}


- (void)happlyValueChange {

    [self play];
}

- (void)hungryValueChange:(NSNotification *)notification {

    [self feed];
    
}

- (void)play {

    NSLog(@"保姆陪小孩玩耍");
    _children.happyValue = 100;
    
}

- (void)feed {

    
    NSLog(@"保姆給小孩晚飯");
    _children.hungryValue = 100;
    
}

- (void)dealloc {

    //移除通知
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"happlyValueNotification" object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"hungryValueNotification" object:nil];
    
    //移除所有通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [_children release];
    
    [super dealloc];
    
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved