【非凡程序员】 OC第十六节课 初识OS X开发二(设置时间和定闹钟练习)

设置时间和定闹钟练习:

 

ViewController.h文件:
#import <Cocoa/Cocoa.h>

@interface ViewController : NSViewController
- (IBAction)shezhijian:(id)sender;
@property (weak) IBOutlet NSTextField *Song;
@property (weak) IBOutlet NSTextField *ShiJian;
@property (nonatomic,strong) NSTimer *time;
@property (weak) IBOutlet NSTextField *SheZhi;
-(void) date;
@end

ViewController.m文件

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad  //-----------构造函数-------------//

 {
    [super viewDidLoad];
    _time=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(date) userInfo:nil repeats:YES];
    }

-(void) date
{
    NSDate *today =[NSDate date];
    NSDateFormatter *f=[NSDateFormatter new];
    NSDateFormatter *f2=[NSDateFormatter new];
    [f setDateFormat:@"yyyy年MM月dd日HH点mm分ss秒"];
    [f2 setDateFormat:@"yyyyMMddHHmmss"];
    NSString *str=[f stringFromDate:today];
    NSString *str2=[f2 stringFromDate:today];
    //NSLog(@"%@",str2);

    [_ShiJian setStringValue:str]; //-----用setStringValue方法吧str给_shijian输出
   
    if ([save isEqual: str2])   //-----判断当str2与设置的时间相同时,闹铃响了
    {
        [_Song setStringValue:@"快起床了 ,主人。。。。。。。"] ;
   }
}

 - (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

NSString *save;
- (IBAction)shezhijian:(id)sender
{
    save = [_SheZhi stringValue];  //---获得设置的时间,给全局变量save;
}
@end

你可能感兴趣的:(非凡程序员)