日常开发小笔记

一、从一个页面pop到指定页面

-(void)back{
 //返回到指定页面(本次连续返回两页)
    NSArray *array = self.navigationController.viewControllers;
    NSLog(@"controllerArray--->%@",array);
    [self.navigationController popToViewController:[array objectAtIndex:1] animated:YES];
}

或者

-(void)back{

for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[AViewController class]]) {
            AViewController *AVC =(AViewController *)controller;
                 [self.navigationController popToViewController:AVC animated:YES];
        }
    }
}

如果退到根视图控制器的话:

[self.navigationController popToRootViewControllerAnimated:YES];

二、创建完tabbar之后,要求第一次出现的页面不是第一个babbar对应的页面

   for (UIViewController *vcc in self.childViewControllers) {
        NSLog(@"-----*-->%@",vcc.title);
        
    }
   self.selectedViewController = self.childViewControllers[3];

三、倒计时按钮抖动的问题

 可以使用等宽字体的方法解决:Courier New、 Courier 是系统等宽字体
 _timeButton.titleLabel.font = [UIFont fontWithName:@"Courier" size:14];

四、导航条背景色透明

  //设置导航条透明度,一句代码搞定!
 [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1];

但是!!!一定要确保self.navigationController.navigationBar.translucent = YES;
上次就因为在AppDelegate里面设置了[[UINavigationBar appearance]setTranslucent:NO];查了半天才找出原因!!!

五、viewWillAppear不响应的问题

1、父类 viewWillAppear不响应----->[super viewWillAppear:animated]没写
2、在UITabBarController使用[self addChildViewController:nvi];
然后 self.selectedViewController = self.childViewControllers[self.selectIdenx];后不执行viewWillAppear

解决办法:

//在该UITabBarController中做以下操作
-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
    //解决使用self.selectedViewController = self.childViewControllers[self.selectIdenx];后对应页面的viewWillAppear不调用的问题!
    [self.selectedViewController viewWillAppear:animated];
}

六、判断当前页面是push还是present过来的

方法一:

-(void)backClick{
    
    NSArray *viewcontrollers = self.navigationController.viewControllers;
    
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {
            //push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    } else{
        //present方式
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

方法二:(推荐)

-(void)backClick{
    //判断当前显示控制器的presentingViewController属性,存在就是modal出来的,为nil就是push进来的
    if (self.presentingViewController) {
        NSLog(@"present方式");
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }else{
        NSLog(@"push方式");
        [self.navigationController popViewControllerAnimated:YES];
    }
}

七、设置按钮文字和图片间距

UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtn.frame = CGRectMake(0, 0, 40, 35);
[leftBtn setImage:[UIImage imageNamed:@"icon_homepage_upArrow"] forState:UIControlStateNormal];
[leftBtn setImage:[UIImage imageNamed:@"icon_homepage_downArrow"] forState:UIControlStateSelected];
 //先设置按钮里面的内容居中
leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//设置文字居左 ->向左移35
[leftBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -35, 0, 0)];
 //设置文字居左 ->向右移30
leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
[leftBtn setTitle:@"上海" forState:UIControlStateNormal];
leftBtn.titleLabel.font = kFONT12;
[leftBtn addTarget:self action:@selector(btn_leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
8476F3A4-4A1D-4A16-910A-11C172D1152E.png

八、价格删除线

UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.bounds.size.width/2, CGRectGetMaxY(titleLabel.frame), cell.bounds.size.width/2, 20)];
NSAttributedString *attrStr = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"  ¥%@",listModel.marketPrice]attributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:13],
       NSForegroundColorAttributeName:[UIColor grayColor],
       NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|NSUnderlinePatternSolid),
       NSStrikethroughColorAttributeName:[UIColor grayColor]}];
priceLabel.attributedText = attrStr;
659FC87C-6B0A-4624-A9B1-CBEC396640A2.png

九、异步加载图片

UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 100, 100)];
[self.view addSubview:imageView1];
UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(20, 150, 100, 100)];
[self.view addSubview:imageView2];
    
NSString *url = @"http://images.apple.com/v/iphone-5s/gallery/a/images/download/photo_1.jpg";
[RequestManager requestWithUrl:url Type:RequestType_GET parameters:nil Success:^(id responseObject) {
        
    //这里responseObject是请求下来的所有图片链接
     //开个多线程异步加载图片
    dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
        //加入全局队列
    dispatch_async(globalQueue, ^{
         UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:responseObject[@"url1"]]]];
            dispatch_async(dispatch_get_main_queue(), ^{
         imageView1.image = image1;
            });
        });
        
    dispatch_async(globalQueue, ^{
       UIImage *image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:responseObject[@"url2"]]]];
            dispatch_async(dispatch_get_main_queue(), ^{
       imageView2.image = image2;
            });
        });
//————————>或者使用SD_image,不需要开启新线程

    } Fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];

十、图片的保存(保存到相册)

UIImageWriteToSavedPhotosAlbum(self.imageView.image, nil, nil, nil);

十一、设置textField的placeholder的颜色和字体

//设置placeholder的颜色和字体
[self.textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

十二、设置Error

NSError *err=[NSError errorWithDomain:@"" code:999 userInfo:@{NSLocalizedDescriptionKey:@"没有更多数据了"}];

获取error

[self showErrorMsg:error.localizedDescription];

十三、xcode调试断点不能停在代码区的解决方案

当我们在开发xcode程序时,往往要用到xcode调试,但由于不小心修改了一些配置信息,而导致在调试时不能追踪到具体的代码区,以下就是解决办法:http://my.oschina.net/u/219482/blog/123031

094421_J96f_219482.png

十四、引入<TencentOpenAPI/TencentOAuth.h>时,使用模拟器会出现Undefined symbols for architecture i386的错误

Undefined symbols for architecture i386:
  "_OBJC_CLASS_$_TencentOAuth", referenced from:
      objc-class-ref in AppDelegate.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

试了很多办法都没有解决,最后在Product---->Scheme---->Edit Scheme在Run下面将Build Configuration中将Release改成Debug就OK了!

十五、隐藏返回按钮(一般退出登录时会用到)

 self.navigationItem.leftBarButtonItem = nil;或
 self.navigationItem.hidesBackButton = YES;

十六、返回码code非字符串的时候的判断方式

if ([responseObject[@"code"] isEqual:@1]) {
//
}

十七、两种方法删除NSUserDefaults所有记录

//方法一
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

//方法二
- (void)resetDefaults{
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict)  {
   [defs removeObjectForKey:key];
}
 [defs synchronize];
}

十八、数组排序

http://blog.csdn.net/daiyelang/article/details/18726947

NSArray *array11 = @[@"2016-10-03 10:29:35",@"2016-10-07 10:29:35",@"2016-10-04 10:29:35",@"2016-10-05 11:29:35",@"2016-10-05 10:29:35"];
NSArray *array22 = [array11 sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"array22----->%@",array22);

打印结果如下:

"2016-10-03 10:29:35",
"2016-10-04 10:29:35",
"2016-10-05 10:29:35",
"2016-10-05 11:29:35",
"2016-10-07 10:29:35"

十九、TableView和collectionView刷新特定的行

//你需要更新的组数   
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:1]; 
[_tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];  
//你需要更新某一组中的cell  
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];  
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil]withRowAnimation:UITableViewRowAnimationNone];

二十、安装Ruby环境时出现的问题

安装步骤按照http://www.jianshu.com/p/c51de465da22 正常安装,如果遇到如下错误:

Skipping update of certificates in '/usr/local/etc/openssl/cert.pem', to force update run:
    rvm osx-ssl-certs update /usr/local/etc/openssl/cert.pem

RVM autolibs is now configured with mode '2' =>
  'Allow RVM to use package manager if found, fail if dependencies are missing. This is default.',
please run `rvm autolibs enable` to let RVM do its job or run and read `rvm autolibs [help]`
or visit https://rvm.io/rvm/autolibs for more information.
Requirements installation failed with status: 1.

执行命令:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

会出现下面的就代码成功:

Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.12/x86_64/ruby-2.3.0.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Skipping update of certificates in '/usr/local/etc/openssl/cert.pem', to force update run:
    rvm osx-ssl-certs update /usr/local/etc/openssl/cert.pem

Requirements installation successful.

如果还有如下错误:

Downloaded archive checksum did not match, archive was removed!
If you wish to continue with not matching download add '--verify-downloads 2' after the command.

There has been an error fetching the ruby interpreter. Halting the installation.

就执行下面的命令:

rvm get head

执行完后会出现:

Upgrade Notes:

  * No new notes to display.

RVM reloaded!

接着执行命令:

rvm cleanup archives

最后执行命令:

rvm install 2.3.0 --debug

问题得到解决,紧接着按照上面链接中ruby -v命令开始执行后面的操作!
http://www.cnblogs.com/chuange-Strongload/p/5891903.html 也可根据这个博客的步骤操作

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容