iOS 一些笔记

来源:互联网 发布:mysql qps 编辑:程序博客网 时间:2024/06/03 21:33

一,获取导航栏相关属性

  1. 获取包含导航栏的高度:
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];    CGRect rectNav = self.navigationController.navigationBar.frame;    CGFloat maxY = rectStatus.size.height+rectNav.size.height;

二,字符串相关

  1. 截取,判断
 NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //1. 根据某个字符串截取成多个子串 NSArray *subA = [msg componentsSeparatedByString:@"hosten_lym"]; //2. 判断是否包含某个字符串  NSRange range1 = [msg rangeOfString:@"hosten_lym"];  if (range1.location != NSNotFound) {      NSLog(@"%@",msg);  }  //3. 替换指定的字符串  subS = [subS stringByReplacingOccurrencesOfString:@"str" withString:@"reStr"];

三, 关于可变数值或字典的一些事情

1,不可变使用 copy 属性防止数组被其他对象修改,可变必须使用 strong 或者 retain ,原因如下:

@property(nonatomic, copy) NSMutableArray *remoteVideoMutArray; _remoteVideoMutArray = [[NSMutableArray alloc] init];[self.remoteVideoMutArray addObject:dic];

这样做会导致一个崩溃,如图:
这里写图片描述
实际上使用copy属性后;这里实际上已经转为不可变的数组,再对其赋值,就会导致上述错误,原理如下:

@property(nonatomic, copy) NSMutableArray *remoteVideoMutArray; _remoteVideoMutArray = [[[NSMutableArray alloc] init] copy];[self.remoteVideoMutArray addObject:dic];

因此,可变必须使用 strong 或者 retain ;

关于控制器那些事

  1. 获取window下rootVC
- (UIViewController *)getCurrentVC{    UIViewController *result = nil;    UIWindow * window = ((AppDelegate*)SYS_DELEGATE).window;    if (window.windowLevel != UIWindowLevelNormal)    {        NSArray *windows = [[UIApplication sharedApplication] windows];        for(UIWindow * tmpWin in windows)        {            if (tmpWin.windowLevel == UIWindowLevelNormal)            {                window = tmpWin;                break;            }        }    }    UIView *frontView = [[window subviews] objectAtIndex:0];    id nextResponder = [frontView nextResponder];    if ([nextResponder isKindOfClass:[UIViewController class]])        result = nextResponder;    else        result = window.rootViewController;    return result;}
  1. 根据rootVC获取最顶层的控制器:
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController{    if ([rootViewController isKindOfClass:[UITabBarController class]]) {        UITabBarController *tabBarController = (UITabBarController *)rootViewController;        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {        UINavigationController* navigationController = (UINavigationController*)rootViewController;        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];    } else if (rootViewController.presentedViewController) {        UIViewController* presentedViewController = rootViewController.presentedViewController;        return [self topViewControllerWithRootViewController:presentedViewController];    } else {        return rootViewController;    }}
0 0
原创粉丝点击