Skip to content

Instantly share code, notes, and snippets.

@congbo
congbo / new_file0
Created September 5, 2015 03:00
MagicalRecord enumerateObjectsUsingBlock
[MagicalRecord saveWithBlock:^(NSManagedObjectContext * localContext) {
NSArray * array = [CoopBoxStorage findAllWithPredicate:predicate inContext:localContext];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
CoopBoxStorage * coopBoxStorage = obj;
[coopBoxStorage MR_deleteEntityInContext:localContext];
}];
} completion:^(BOOL success, NSError * error) {
if (success) {
DDLogInfo(@"CoopBoxStorage stored on database without errors");
@congbo
congbo / imageWithUIView
Created August 31, 2015 01:09
UIView 转 UIImage
- (UIImage *) imageWithUIView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@congbo
congbo / new_file0
Created March 19, 2015 01:45
iOS8 UIAlertController弹出框中添加视图(例如日期选择器等等)。相关第三方库:ActionSheetPicker-3.0
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
datePicker.minimumDate = [NSDate date];
datePicker.maximumDate = [NSDate dateWithTimeInterval:(24*60*60*2) sinceDate:datePicker.minimumDate];
datePicker.minuteInterval = 10;
datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文显示
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n" message:nil   preferredStyle:UIAlertControllerStyleActionSheet];
[alert.view addSubview:datePicker];
@congbo
congbo / new_file0
Created March 18, 2015 03:17
xcode 上 A -> navB -> B,A弹出的modal视图B需要显示导航栏,于是B embed 一个navigationController navB,这样A 需要传值给B时中间隔了一个navB
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"0"]) {
UINavigationController * nav = segue.destinationViewController;
LaunchTongChengTableViewController * vc = (LaunchTongChengTableViewController *)nav.topViewController;
if ([vc respondsToSelector:@selector(setScenarioType:)]) {
vc.scenarioType = 0;
}
}
}
@congbo
congbo / isModal
Last active August 29, 2015 14:16
判断当前viewcontroller是push还是modal展示的
- (BOOL)isModal {
return self.presentingViewController.presentedViewController == self
|| self.navigationController.presentingViewController.presentedViewController == self.navigationController
|| [self.tabBarController.presentingViewController isKindOfClass:[UITabBarController class]];
}
@congbo
congbo / getMainStoryboard
Created August 4, 2014 03:39
getMainStoryboard
- (UIStoryboard *)mainStoryboard
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main"
bundle:[NSBundle mainBundle]];
return storyboard;
}
@congbo
congbo / addTask
Created May 15, 2014 09:50
弹出带输入框的alertView
- (void)addTaskButtonClicked:(id)sender
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"NewTaskTitle", @"")
message:@""
delegate:self
cancelButtonTitle:NSLocalizedString(@"NewTaskCancel", @"")
otherButtonTitles:NSLocalizedString(@"NewTaskConfirm", @""), nil];
alertView.delegate = self;
alertView.tag = 0;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
@congbo
congbo / charNumber
Last active August 8, 2018 03:23
计算NSString中英文字符串的字符长度,及按字符长度截取
//计算NSString中英文字符串的字符长度。ios 中一个汉字算2字符数
- (int)charNumber:(NSString*)strtemp
{
int strlength = 0;
char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
for (int i = 0; i < [strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding]; i++) {
if (*p) {
p++;
strlength++;
} else {
@congbo
congbo / getDateByFormat
Created April 2, 2014 08:24
格式化时间,如忽略秒
+ (NSDate*)_getDateBySecondFormat:(NSDate*)date
{
return [self _getDateByFormat:date
Format:@"yyyy/MM/dd HH:mm"];
}
+ (NSDate*)_getDateByFormat:(NSDate*)date Format:(NSString*)format
{
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:format];