一、从一个页面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];
八、价格删除线
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;
九、异步加载图片
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
十四、引入时,使用模拟器会出现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 也可根据这个博客的步骤操作