一、前言
已经入职一个多月了,收获不多不少,拿来作为点滴记录,不然做过的东西总是忘掉,又等于没做,重复造轮子很累啊~~
二、连续签到知识点总结
1、单例
什么时候该使用单例
1. 可以保证的程序运行过程,一个类只有一个示例,而且该实例易于供外界访问
2. 从而方便地控制了实例个数,并节约系统资源
+ (instancetype)shareInstance{
static class *__instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__instance = [[SignLocNotifManager alloc] init];
[__instance setup];
});
return __instance;
}
3、监听者模式
// 发出进入主界面的通知
[[NSNotificationCenter defaultCenter] postNotificationName:KYApplicationEnterMainPageNotification object:nil];
+ (instancetype)shareInstance{
static SignLocNotifManager *__instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__instance = [[SignLocNotifManager alloc] init];
[__instance setup];
});
return __instance;}
-(void)setup{
//添加监听。完全终止进程后,可以通过点击通知进入App进行签到
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushViewControllerToDuibaViewControllerByObserver) name:KYApplicationEnterMainPageNotification object:nil];}
4、签到主逻辑
-(void)setLocalNotificationsBySignTime{
if ([self isFirstSign]){
[self cancelLocalNotificationWithKey:EightPMNotification];
[self sign];
}else if([self isValidDailySign]){
[self cancelLocalNotificationWithKey:EveryDayNotification];
[self sign];
}else{
}}
5、捕获luanchOption
-(void)setLocalNotificationByLaunchOptions:(NSDictionary *)launchOptions{
if (launchOptions) {
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
NSDictionary *localUserInfo = [notification userInfo];
if ([[localUserInfo objectForKey:EightPMNotification] isEqualToString:EightPMNotification]||[[localUserInfo objectForKey:EveryDayNotification] isEqualToString:EveryDayNotification]) {
self.launchOptions = launchOptions;
NSLog(@"YYDebug:通过点击终止的App推送签到");
[self setLocalNotificationsBySignTime];
}
}}
6、UIAlert使用
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"获取天气失败,请重试" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
QueryCityWeatheVC *scVC = [[QueryCityWeatheVC alloc] init];
[self presentViewController:scVC animated:NO completion:nil];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了取消");
}]];
[self presentViewController:alert animated:YES completion:nil];
7、UIMenuItem使用
注:长按cell弹出menu
1)自定义的cell中添加如下方法
-(BOOL)canBecomeFirstResponder{
return YES;}
2)具体使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CityTVC *cell = [[CityTVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
[cell addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)]];
NSArray *array = [[NSUserDefaults standardUserDefaults] valueForKey:@"history"];
cell.textLabel.text = array[indexPath.row];
return cell;
}
-(void)longTap:(UILongPressGestureRecognizer*)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan) {
CityTVC *cell = (CityTVC *)recognizer.view;
[cell becomeFirstResponder];
UIMenuController *menu = [UIMenuController sharedMenuController];
UIMenuItem *deleteItem = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(deleteItemClick:)];
UIMenuItem *setReminderItem = [[UIMenuItem alloc] initWithTitle:@"设置提醒" action:@selector(setReminderClick:)];
[menu setMenuItems:@[deleteItem,setReminderItem]];
[menu setTargetRect:cell.frame inView:self.view];
[menu setMenuVisible:YES animated:YES];
}
}
-(void)deleteItemClick:(id)sender{
NSInteger index = [self.cityTableView indexPathForSelectedRow].row;
NSArray *array = [[NSUserDefaults standardUserDefaults] valueForKey:@"history"];
NSMutableArray *newArray = [NSMutableArray array];
for (int i = 0; i<array.count; i++) {
if (i!=index) {
[newArray addObject:array[i]];
}
}
[[NSUserDefaults standardUserDefaults] setObject:newArray forKey:@"history"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.cityTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(void)setReminderClick:(id)sender{
}
8、从Plist文件读取数据并均匀排布
-(void)initHotCitiesView{
NSString *path = [[NSBundle mainBundle] pathForResource:@"hotCities.plist" ofType:nil];
NSArray *cityArray = [NSArray arrayWithContentsOfFile:path];
float btnWidth = Width/7.0;
float btnHeight = Height/13.0;
int row = 0 ,col = 0;
int index = 0;
for (int i = 1;i<cityArray.count;i++) {
float btnX = 2*btnWidth * col;
float btnY = 2*btnHeight * row;
index = i % 4;
if (index == 0) {
row = row + 1;
col = 0 ;
}else{
col = col + 1;
}
//计算每个btn 的位置和大小
UIButton *cityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(btnX, btnY, btnWidth, btnHeight);
[cityBtn setFrame:frame];
[cityBtn setTitle:cityArray[i] forState:UIControlStateNormal];
cityBtn.backgroundColor = [UIColor grayColor];
[cityBtn sizeToFit];
[cityBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:cityBtn];
}}
9、四种加载tableView方式
1)常规
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *idfity=[NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row];
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:idfity];
if (!cell) {
cell=[[UITableViewCell alloc]initWithStyle:0 reuseIdentifier:idfity];
}
return cell;}
2)tableview registerClass 的方式加载
viewdidLoad中加入如下方法
[tableView registerClass:[TableViewCell class] forCellReuseIdentifier:id];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:id];
if (cell == nil) {
}
return cell; }
3)tableView registerNib
viewdidload中加入
[tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:IDENTIFIER];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:id];
if (cell == nil) {
}
return cell; }
4)xib文件加载
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
if (cell == nil) {
cell = [[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:nil options:nil].firstObject;
}
return cell; }