整理关于Cocoa编程的一些有用的函数
最近貌似爱上写cocoa代码了 做了一个小程序
现将一些要用到的内容标记一下~ 以备不时之用。
定时刷新
[NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; //定时刷新所用到的函数 - (void)timerFired:(id)sender { [self reflash:1]; }
读取plist
因为在沙盒中不能进行写,所以要读取的是另一个
//获取应用程序沙盒的Documents目录 NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *plistPath1 = [paths objectAtIndex:0]; //得到完整的文件名 NSString *filename=[plistPath1 stringByAppendingPathComponent:@"config.plist"]; //分配空间 NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filename]; //取得值 NSInteger lasttid = [ data objectForKey:@"lasttid" ];
写plist
NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *plistPath1 = [paths objectAtIndex:0]; //得到完整的文件名 NSString *filename=[plistPath1 stringByAppendingPathComponent:@"config.plist"]; //分配空间 NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filename]; //取得值 NSInteger lasttid = [ data objectForKey:@"lasttid" ]; [data setObject:tmp forKey:@"lasttid" ]; [data writeToFile:filename atomically:YES];
通知中心
NSUserNotification *notification = [[NSUserNotification alloc] init]; notification.title = title; notification.subtitle = subtitle; notification.informativeText = informativeText; //只有当用户设置为提示模式时,才会显示按钮 notification.hasActionButton = YES; notification.actionButtonTitle = @"OK"; notification.otherButtonTitle = @"Cancel"; //递交通知 [[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notification]; //设置通知的代理 [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
删除通知
//删除已经显示过的通知(已经存在用户的通知列表中的) [[NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications]; //删除已经在执行的通知(比如那些循环递交的通知) for (NSUserNotification *notify in [[NSUserNotificationCenter defaultUserNotificationCenter] scheduledNotifications]) { [[NSUserNotificationCenter defaultUserNotificationCenter] removeScheduledNotification:notify]; }
添加微标数字
[[NSApp dockTile] setBadgeLabel:[NSString stringWithFormat:@"%ld", row]];
移除微标数字
- (void)removeBadge { [[NSApp dockTile] setBadgeLabel:nil]; }
用函数来实现小数据的读写
//保存数据在某个文件中,一般在/Users/用户名/Library/Preferences/下 [[NSUserDefaults standardUserDefaults] setInteger:newid forKey:@"id"]; //获得已经保存的内容 NSInteger lasttid = [[NSUserDefaults standardUserDefaults] integerForKey:@"id"];
窗口关闭后能继续打开窗口:
头文件内加入
@property (assign) IBOutlet NSWindow *window;
实现类
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag { if (!flag){ [self.window makeKeyAndOrderFront:self]; return YES; } return NO; }
【声明】本文 整理关于Cocoa编程的一些有用的函数 为柠之漠然原创文章,转载请注明出自
枫之落叶
并保留本文有效链接:https://blog.shiniv.com/2013/06/organize-some-functions-on-cocoa-programming/ , 转载请保留本声明!