2.怎麼從底部彈出一個框? 兩種方法: 一種用 UIActionSheet 另一種用 UIAlertController
1 // 1.把帖子發布時間字符串轉換成(NSDate)日期對象 => NSDateFormatter 2 // NSDateFormatter:NSStreing與NSDate互轉 3 NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; 4 // 設置格式 5 fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; 6 // 發布日期對象 7 NSDate *createDate = [fmt dateFromString:item.create_time]; 8 9 // 獲取日歷類對象 10 // NSCalendar *calendar = [NSCalendar currentCalendar]; 11 // 獲取日期組件 12 // NSDateComponents *cmp = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:createDate toDate:[NSDate date] options:NSCalendarWrapComponents]; 13 14 NSString *timeStr = item.create_time; 15 if ([createDate isThisYear]) { 16 if ([createDate isThisToday]) { 17 18 // 獲取日期差值 19 if (cmp.hour >= 1) { 20 timeStr = [NSString stringWithFormat:@"%ld小時前",cmp.hour]; 21 } else if (cmp.minute >= 2) { 22 timeStr = [NSString stringWithFormat:@"%ld分鐘前",cmp.minute]; 23 } else { // 剛剛 24 timeStr = @"剛剛"; 25 } 26 } else if ([createDate isThisYesterday]) { // 昨天 27 // 昨天 21:10 28 fmt.dateFormat = @"昨天 HH:mm"; 29 timeStr = [fmt stringFromDate:createDate]; 30 31 } else { // 昨天之前 08-05 21:10:08 32 fmt.dateFormat = @"MM-dd HH:mm:ss"; 33 timeStr = [fmt stringFromDate:createDate]; 34 } 35 } 36 37 因為判斷日期要常用,我們就把判斷方法抽取到NSDate的分類 38 39 - (BOOL)isThisYear 40 { 41 // 獲取當前日期對象 42 NSDate *curDate = [NSDate date]; 43 // 獲取日歷類 44 NSCalendar *curCalendar = [NSCalendar currentCalendar]; 45 // 獲取自己日期組件(年,月,等) self代碼外面調用這個方法的日歷類(也就是createDate) 46 NSDateComponents *selfCmp = [curCalendar components:NSCalendarUnitYear fromDate:self]; 47 // 獲取當前時間日期組件(年,月,等) 48 NSDateComponents *curCmp = [curCalendar components:NSCalendarUnitYear fromDate:curDate]; 49 return curCmp.year == selfCmp.year; 50 } 51 52 // 判斷是否是今天 53 - (BOOL)isThisToday 54 { 55 // 獲取日歷類 56 NSCalendar *curCalendar = [NSCalendar currentCalendar]; 57 return [curCalendar isDateInToday:self]; 58 } 59 60 - (BOOL)isThisYesterday 61 { 62 NSCalendar *curCalendar = [NSCalendar currentCalendar]; 63 return [curCalendar isDateInYesterday:self]; 64 }