小代码合集
解决block中self的引用
__weak typeof(self) weakSelf = self
iOS获得屏幕大小
//宽度:
UIScreen mainScreen] bounds].size.width
//高度:
UIScreen mainScreen] bounds].size.height
//可以将屏幕大小定义成一个宏:
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
延时执行可以尝试NSTimer方法
例子:UIAlertViewController自动消失
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有上一部了" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:NO completion:nil];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(creatAlert:) userInfo:alert repeats:NO];
- (void)creatAlert:(NSTimer *)timer {
UIAlertController *alert = [timer userInfo];
[alert dismissViewControllerAnimated:YES completion:nil];
alert = nil;
}
定制statusBar部分
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
NSString 和 NSNumber
NSString *str = @"120".
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *numTemp = [numberFormatter numberFromString:str];
NSNumber *num = [NSNumber numberWithInt:2] ;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSString* str = [numberFormatter stringFromNumber:num];
UINavigationController先pop再push,push操作将无效。
解决方案是:
1. 从self.navigationController.viewControllers中拿出需要保留的View Controller放到一个数组中
2. 向数组中添加需要push的View Controller
3. 调用[self.navigationController setViewControllers:array animated:YES]
InvestmentViewController *investmentList = [[InvestmentViewController alloc] init];
investmentList.hidesBottomBarWhenPushed = YES;
NSMutableArray *arrView = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
int index = (int)[arrView indexOfObject:self];
[arrView removeObjectAtIndex:index];
[arrView addObject:investmentList];
[self.navigationController setViewControllers:arrView animated:YES];