今天偶然发现mac的应用程序和iphone一样,都是用的oc写的,然后就按照iphone的方式制作了一个小小的加班计,功能很简单,就三个按钮,主要是用来记录平时加班的,这里给出源码参考。

Demo源码下载

GitHub地址:https://github.com/DamonHu/jiabanji

GitOSC下载地址:http://git.oschina.net/DamonHoo/jiabanji

Demo程序下载

百度云盘下载:链接: http://pan.baidu.com/s/1dEAcslN 密码: 8pqr


实现功能:


屏幕快照 2015-08-04 22.52.57.png

屏幕快照 2015-08-04 22.53.45.png


1.不同月份创建不同的文档便于管理

2.记录平时加班

3.记录节假日加班情况


源码是这样的:

在AppDelegate加上下面这句话,是为了点击左上角的叉号退出程序用的


- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
    return YES;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    NSButton *button = [[NSButton alloc]initWithFrame:NSRectFromCGRect(CGRectMake(180, 150, 100, 50))];
    [button setImage:[NSImage imageNamed:@"jiaban.png"]];
    [button setTarget:self];
    [button setTag:1];
    [button setAction:@selector(addWorkTime:)];
    [self.view addSubview:button];
    NSButton *button2 = [[NSButton alloc]initWithFrame:NSRectFromCGRect(CGRectMake(80, 50, 110, 40))];
    [button2 setTitle:@"节假日上班"];
    [button2 setTarget:self];
    [button2 setTag:2];
    [button2 setAction:@selector(addWorkTime:)];
    [self.view addSubview:button2];
    NSButton *button3 = [[NSButton alloc]initWithFrame:NSRectFromCGRect(CGRectMake(280, 50, 110, 40))];
    [button3 setTitle:@"节假日下班"];
    [button3 setTarget:self];
    [button3 setTag:3];
    [button3 setAction:@selector(addWorkTime:)];
    [self.view addSubview:button3];
    
    // Do any additional setup after loading the view.
}
- (void)addWorkTime:(NSButton*)sender
{
    NSDateFormatter *form=[[NSDateFormatter alloc]init];
    [form setDateFormat:@"yyyy-MM"];
    NSString *date=[form stringFromDate:[NSDate date]];
    self.path=[NSString stringWithFormat:@"/Users/admin/%@.txt",date];//这里需要改为自己的主目录
    NSFileManager *filemanager=[NSFileManager defaultManager];
    if ([filemanager fileExistsAtPath:self.path]) {
        //NSLog(@"已经存在");
    }
    else
    {
        NSData *data=[[NSString stringWithFormat:@"%@记录\n\n",date] dataUsingEncoding:NSUTF8StringEncoding];
      bool succeed =  [filemanager createFileAtPath:self.path contents:data attributes:nil];
        if (succeed) {
            //NSLog(@"创建成功");
        }
        else
        {
            //NSLog(@"创建失败");
            NSAlert *alert = [[NSAlert alloc]init];
            [alert addButtonWithTitle:@"确定"];
            [alert setMessageText:@"创建文件失败"];
            [alert setAlertStyle:NSWarningAlertStyle];
            [alert runModal];
            return;
        }
    }
    [self writeFile:sender.tag];
}
-(void)writeFile:(NSInteger)tag
{
     NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    if (tag==1) {
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss EEEE\n"];
    }
    else if (tag==2)
    {
       [formatter setDateFormat:@"\n\t\tyyyy-MM-dd EEEE\n上班时间:HH:mm:ss "];
    }
    else if (tag==3)
    {
        [formatter setDateFormat:@" ==== 下班时间:HH:mm:ss\n\n"];
    }
    NSString *timeStr=[formatter stringFromDate:[NSDate date]];
    NSData *data=[timeStr dataUsingEncoding:NSUTF8StringEncoding];
    NSFileHandle *handle=[NSFileHandle fileHandleForWritingAtPath:self.path];
    [handle seekToEndOfFile];
    [handle writeData:data];
    [handle closeFile];
    NSAlert *alert = [[NSAlert alloc]init];
    [alert addButtonWithTitle:@"确定"];
    [alert setMessageText:@"加班记录成功"];
    [alert setAlertStyle:NSWarningAlertStyle];
    [alert runModal];
}


2015-08-19续

add 增加了自定义mac用户的地址,这样就可以修改文件保存路径了

add 增加删除路径

屏幕快照 2015-08-19 19.04.56.png

就是增加了两个函数:

增加路径:


self.path = [[NSUserDefaults standardUserDefaults] stringForKey:@"txtpath"];
    if ([self.path isEqualToString:@""]||!self.path) {
        self.textField=[[NSTextField alloc]initWithFrame:NSRectFromCGRect(CGRectMake(180, 230, 100, 30))];
        [self.view addSubview:self.textField];
        NSButton *button = [[NSButton alloc]initWithFrame:NSRectFromCGRect(CGRectMake(300, 230, 80, 30))];
        [button setTag:10];
        [button setTitle:@"保存"];
        [button setTarget:self];
        [button setAction:@selector(setAdminPath:)];
        [self.view addSubview:button];
    }

-(void)setAdminPath:(NSButton*)sender
{
    if ([self.textField.stringValue isEqualToString:@""]) {
        NSAlert *alert = [[NSAlert alloc]init];
        [alert addButtonWithTitle:@"确定"];
        [alert setMessageText:@"请输入mac用户名称"];
        [alert setAlertStyle:NSWarningAlertStyle];
        [alert runModal];
    }
    else
    {
    self.path=self.textField.stringValue;
    [[NSUserDefaults standardUserDefaults] setValue:self.path forKey:@"txtpath"];
    [sender setHidden:true];
    [self.textField setHidden:true];
    }
}


删除路径:


-(void)clearAdminPath
{
    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"txtpath"];
    [self viewDidLoad];
}




☟☟可点击下方广告支持一下☟☟

最后修改:2024 年 02 月 27 日
请我喝杯可乐,请随意打赏: ☞已打赏列表