兼职iOS开发已有一个多月了 记一下这一个多月里用到的方法 iOS小白的不定期更新
1.A页面不同方法调B页面 跳转时修改B页面的列表点击方法
在B页面的.h文件中声明点击方法
@property (nonatomic, copy) void (^didSelectItem)(DataList *item);
@property (nonatomic, strong) NSMutableArray<DataList *> *dataList;
A页面的跳转方法
重写点击事件
BController *vb = [BController new];
vb.didSelectItem = ^(DataList *item) {
CController *vc= [CController new];
[self.navigationController pushViewController:vc animated:YES];
};
[self.navigationController pushViewController:vb animated:YES];
}
B页面方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (self.didSelectItem) {
self.didSelectItem(self.dataProvider.dataList[indexPath.row]);
}
else {
//默认方法
}
}
2.判断字符串中是否含有某字符串
if ([str rangeOfString:@"a"].location == NSNotFound) {
//不包含
} else {
//包含
}
字符串开始包含有某字符串
if([str hasPrefix:@"a"]) {
//包含
}else{
//不包含
}
字符串结束包含有某字符串
if([str hasSuffix:@"a"]) {
//包含
}else{
//不包含
}
3.将JSON串转化为字典或者数组
- (NSArray *)arrayWithJsonString:(NSString *)jsonString{
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
return array;
}
4.计算天数后的新日期
- (NSString *)computeDateWithDays:(NSInteger)days date:(NSString *)date
{
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *myDate = [dateFormatter dateFromString:date];
NSDate *newDate = [myDate dateByAddingTimeInterval:60 * 60 * 24 * days];
return [dateFormatter stringFromDate:newDate];
}