// // BaseViewController.m // IBOSS // // Created by iHope on 14-7-7. // Copyright (c) 2014年 elongtian. All rights reserved. // #import "BaseViewController.h" #include #include @interface BaseViewController () @end @implementation BaseViewController @synthesize hud; @synthesize mlbTitle; @synthesize delegate; @synthesize OnGoBack; @synthesize mTopColor; @synthesize mTopImage; @synthesize mLoadMsg; @synthesize mTitleColor; @synthesize mbLightNav; @synthesize mFontSize; /** viewDidLoad */ - (void)viewDidLoad { [super viewDidLoad]; hud = [[MBProgressHUD alloc] initWithView:self.view] ; [self.view addSubview:hud]; // [self.view bringSubviewToFront:hud]; self.view.backgroundColor = [UIColor colorWithWhite:0.96 alpha:1.0]; if (IOS_7) { //去掉边缘化 适配 self.edgesForExtendedLayout = UIRectEdgeNone; self.extendedLayoutIncludesOpaqueBars = NO; self.automaticallyAdjustsScrollViewInsets = NO; if (!mbLightNav) { self.navigationController.navigationBar.translucent = NO; } } [self refreshNavColor]; /* mlbTitle = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, self.view.frame.size.width-100, 44)]; mlbTitle.backgroundColor = [UIColor clearColor]; mlbTitle.font = [UIFont fontWithName:@"Helvetica-Bold" size:mFontSize]; mlbTitle.textAlignment = NSTextAlignmentCenter; mlbTitle.textColor = self.mTitleColor; mlbTitle.text = self.title; self.navigationItem.titleView = mlbTitle; */ [self addLeftImageBtn:nil target:nil action:nil]; [self addRightImageBtn:nil target:nil action:nil]; } /** viewWillAppear @param animated <#animated description#> */ - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // [SelectTabBar Share].hidden = YES; } /** didReceiveMemoryWarning */ - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /** dealloc */ - (void)dealloc { self.mTopColor = nil; self.mTopImage = nil; self.mLoadMsg = nil; } /** 初始化 @param nibNameOrNil <#nibNameOrNil description#> @param nibBundleOrNil <#nibBundleOrNil description#> @return <#return value description#> */ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { mbLightNav = YES; mFontSize = 20; self.mTopImage = [UIImage imageNamed:IOS_7?@"topbar7.png":@"topbar.png"]; } return self; } /** 将数据转换成字符串 @param arr <#arr description#> @return <#return value description#> */ - (NSString *)getArrToString:(NSArray *)arr { if(!arr) return nil; NSMutableString *totalstr=[[NSMutableString alloc] init]; for(int i=0;i */ - (NSString *)IPAddress { NSString *address = @"error"; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; // retrieve the current interfaces - returns 0 on success success = getifaddrs(&interfaces); if (success == 0) { // Loop through linked list of interfaces temp_addr = interfaces; while (temp_addr != NULL) { if( temp_addr->ifa_addr->sa_family == AF_INET) { // Check if interface is en0 which is the wifi connection on the iPhone if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) { // Get NSString from C String address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; } } temp_addr = temp_addr->ifa_next; } } // Free memory freeifaddrs(interfaces); return address; } /** 设备信息 */ - (void)getDeviceAndOSInfo { //here use sys/utsname.h struct utsname systemInfo; uname(&systemInfo); //get the device model and the system version NSLog(@"%@", [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]); } /** 标题 @param text <#text description#> */ - (void)showTitle:(NSString*)text{ self.navigationItem.title = text; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0, 15, 18); self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button]; } /** 标题 @param text <#text description#> @param nav <#nav description#> */ - (void)showTitle:(NSString*)text navi:(UINavigationController*)nav{ nav.navigationItem.title = text; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0, 15, 18); nav.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button]; } /** 文本加载中 @param text <#text description#> */ - (void)showLoadingText:(NSString*)text { // self.hud=nil; // [hud release]; //如果设置此属性则当前的view置于后台 hud.dimBackground = YES; //设置对话框文字 hud.labelText = text; [hud show:YES]; } /** 进度条加载中。。。。 */ - (void)showLoading { hud.dimBackground = YES; hud.labelText = @"正在加载"; [hud show:YES]; } /** 进度条隐藏 */ - (void) hideLoading { [hud hide:YES]; } /** 显示消息 @param text <#text description#> */ - (void)showAlertViewText:(NSString *)text { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:text preferredStyle:UIAlertControllerStyleAlert ]; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ }]]; // 3.显示alertController:presentViewController [self presentViewController:alert animated:YES completion:nil]; } /** 显示消息 @param text <#text description#> */ - (void)showAlertViewBackText:(NSString *)text { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:text preferredStyle:UIAlertControllerStyleAlert ]; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ }]]; // 3.显示alertController:presentViewController [self presentViewController:alert animated:YES completion:nil]; } /** 返回 */ - (void)goHome { [self.navigationController popToRootViewControllerAnimated:YES]; } /** 返回 */ - (void)goBack { if (delegate && OnGoBack) { if ([delegate respondsToSelector:OnGoBack] ) { [delegate performSelector:@selector(OnGoBack) withObject:self]; } } if (self.navigationController) { [self.navigationController popViewControllerAnimated:YES]; } else { [self dismissViewControllerAnimated:YES completion:nil]; } } /** 刷新导航单颜色 */ - (void)refreshNavColor { //导航栏背景色 if (@available(iOS 15.0, *)) { UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init]; appearance.backgroundColor = NavigationBarColor; appearance.backgroundEffect = nil; appearance.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; self.navigationController.navigationBar.scrollEdgeAppearance = appearance; self.navigationController.navigationBar.standardAppearance = appearance; self.navigationController.navigationBar.hidden = NO; }else{ [self.navigationController.navigationBar setBackgroundImage:mTopImage forBarMetrics:UIBarMetricsDefault]; //设置导航栏文字颜色 [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; } self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; } /** 导航栏清空 */ - (void)clearNavItem { self.navigationItem.leftBarButtonItem = nil; self.navigationItem.rightBarButtonItem = nil; } /** 图片按钮 @param image <#image description#> @param target <#target description#> @param action <#action description#> @return <#return value description#> */ - (UIButton *)getImageButton:(UIImage *)image target:(id)target action:(SEL)action { UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; rightBtn.frame = CGRectMake(0, 0, 40, 40); [rightBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; int iWidth = image.size.width/2; int iHeight = image.size.height/2; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; imageView.frame = CGRectMake((40-iWidth)/2, (40-iHeight)/2, iWidth, iHeight); imageView.tag = 1300; [rightBtn addSubview:imageView]; return rightBtn; } /** UIBarButtonItem @param image <#image description#> @param target <#target description#> @param action <#action description#> @return <#return value description#> */ - (UIBarButtonItem *)getImageBarItem:(UIImage *)image target:(id)target action:(SEL)action { //int iBtnWidth = IOS_7?40:40; UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; //rightBtn.frame = CGRectMake(0, 0, 40, 20); [rightBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; [rightBtn setBackgroundImage:image forState:UIControlStateNormal]; rightBtn.frame = CGRectMake(0, 0, 44, 20); // int iWidth = image.size.width/2; // int iHeight = image.size.height/2; // UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; // imageView.frame = CGRectMake((iBtnWidth-iWidth)/2, (40-iHeight)/2, iWidth, iHeight); // imageView.tag = 1300; // [rightBtn addSubview:imageView]; return [[UIBarButtonItem alloc] initWithCustomView:rightBtn] ; } /** UIBarButtonItem @param name <#name description#> @param target <#target description#> @param action <#action description#> @return <#return value description#> */ - (UIBarButtonItem *)getTextBarItem:(NSString *)name target:(id)target action:(SEL)action { UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; rightBtn.frame = CGRectMake(0, 0, 40, 40); [rightBtn setTitle:name forState:UIControlStateNormal]; [rightBtn setTitleColor:self.mTitleColor forState:UIControlStateNormal]; rightBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [rightBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; return [[UIBarButtonItem alloc] initWithCustomView:rightBtn] ; } /** 航栏左侧图片 @param image <#image description#> @param target <#target description#> @param action <#action description#> */ - (void)addLeftImageBtn:(UIImage *)image target:(id)target action:(SEL)action { self.navigationItem.leftBarButtonItem = [self getImageBarItem:image target:target action:action]; } /** 航栏右侧图片 @param image <#image description#> @param target <#target description#> @param action <#action description#> */ - (void)addRightImageBtn:(UIImage *)image target:(id)target action:(SEL)action { self.navigationItem.rightBarButtonItem = [self getImageBarItem:image target:target action:action]; } /** 航栏右侧图片数组 @param array <#array description#> */ - (void)addRightImageBtns:(NSArray *)array { if (!array || array.count == 0) { return; } UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, array.count*40, 40)] ; for (int i = 0; i < array.count; i ++) { UIButton *btn = [array objectAtIndex:i]; btn.frame = CGRectMake(i*40, 0, 40, 40); [view addSubview:btn]; } self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view] ; } /** 导航栏右侧文本 @param name <#name description#> @param target <#target description#> @param action <#action description#> */ - (void)addRightTextBtn:(NSString *)name target:(id)target action:(SEL)action { self.navigationItem.rightBarButtonItem = [self getTextBarItem:name target:target action:action]; } /** 标题 @param title <#title description#> */ - (void)setTitle:(NSString *)title { [super setTitle:title]; mlbTitle.text = title; } /** UIView @return <#return value description#> */ - (UIView *)getInputAccessoryView { UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)] ; inputView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(inputView.frame.size.width-50, 0, 50, inputView.frame.size.height); [btn setTitle:@"隐藏" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont systemFontOfSize:16]; [btn addTarget:self action:@selector(onHideKeyboard) forControlEvents:UIControlEventTouchUpInside]; [inputView addSubview:btn]; return inputView; } /** 隐藏键盘 */ - (void)onHideKeyboard { [self.view endEditing:NO]; } /** 显示logo @param iOffset <#iOffset description#> */ - (void)showLogo:(int)iOffset { if (mLogoView) { return; } int iWidth = 150; int iHeight = 130; mLogoView = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-iWidth)/2, (self.view.frame.size.height-iHeight)/2+iOffset, iWidth, iHeight)]; mLogoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin; mLogoView.image = [UIImage imageNamed:@"default_logo.png"]; [self.view addSubview:mLogoView]; } /** 隐藏logo */ - (void)hideLogo { if (mLogoView) { [mLogoView removeFromSuperview]; mLogoView = nil; } } /** 加载进度条开始 */ - (void)startLoading { if (mLoadView) { return; } mLoadView = [[MBProgressHUD alloc] initWithView:self.view]; if (mLoadMsg) { mLoadView.mode = MBProgressHUDModeCustomView; mLoadView.labelText = mLoadMsg; } [self.view addSubview:mLoadView]; // 判断加载框是否显示 [UserInfoManager Share].isShowLoading=YES; [mLoadView show:YES]; } /** 加载进度条结束 */ - (void)stopLoading { [UserInfoManager Share].isShowLoading=NO; [mLoadView hide:YES]; mLoadView = nil; } /** 加载进度条开始 新版1.0.0 */ - (void)startNewLoading { _mNewLoadView = [NewMBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Change the background view style and color. _mNewLoadView.backgroundView.style = NewMBProgressHUDBackgroundStyleSolidColor; _mNewLoadView.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.1f]; // 判断加载框是否显示 [UserInfoManager Share].isShowLoading=YES; } /** 加载进度条结束 新版1.0.0 */ - (void)stopNewLoading { [UserInfoManager Share].isShowLoading=NO; //[_mNewLoadView hide:YES]; [_mNewLoadView hideAnimated:YES]; _mNewLoadView = nil; } /** 显示消息 @param msg <#msg description#> */ - (void)showMsg:(NSString *)msg { mLoadView = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:mLoadView]; mLoadView.mode = MBProgressHUDModeCustomView; mLoadView.labelText = msg; [mLoadView show:YES]; [mLoadView hide:YES afterDelay:1]; mLoadView = nil; } /** md5加密 @param str <#str description#> @return <#return value description#> */ - (NSString *) md5:(NSString *)str { const char *cStr = [str UTF8String]; unsigned char result[16]; CC_MD5( cStr, (CC_LONG)strlen(cStr), result ); return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ]; } /** * 异常登录 */ - (void)showReLoginDialog{ __weak id weakapp=Appdelegate; __weak id userShare=[UserInfoManager Share] ; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"异常登录" message:@"此用户已在其他移动端登录" preferredStyle:UIAlertControllerStyleAlert ]; [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ [userShare ClearUserData]; [weakapp changeViewDidLogout]; }]]; // 3.显示alertController:presentViewController [self presentViewController:alert animated:YES completion:nil]; } /** * 异常登录 */ - (void)showReLoginDialog:(NSString *)message { __weak id weakapp=Appdelegate; __weak id userShare=[UserInfoManager Share] ; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"异常登录" message:message preferredStyle:UIAlertControllerStyleAlert ]; [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ [userShare ClearUserData]; [weakapp changeViewDidLogout]; }]]; // 3.显示alertController:presentViewController [self presentViewController:alert animated:YES completion:nil]; } /** 无数据的view @param frame <#frame description#> @return <#return value description#> */ - (UIView *)noDataViewByFrame:(CGRect)frame{ UIView *noDataView = [[UIView alloc]init]; noDataView.frame = frame; UIImageView *nodataImgView = [[UIImageView alloc] initWithFrame:CGRectMake(noDataView.frame.size.width/2-16,noDataView.frame.size.height/2-16,32,32)]; [nodataImgView setImage:[UIImage imageNamed:@"icon_no_data"]]; [noDataView addSubview:nodataImgView]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(noDataView.frame.size.width/2-16-12,CGRectGetMaxY(nodataImgView.frame)+3,70, 25)]; label.font = [UIFont systemFontOfSize:NoDataFontOfSize]; label.text = @"无数据"; label.numberOfLines = 2; label.textColor = [UIColor lightGrayColor]; [noDataView addSubview:label]; return noDataView; } /** 背景提示布局 @param frame <#frame description#> @param str <#str description#> @return <#return value description#> */ - (UIView *)backGroundPromptViewByFrame:(CGRect)frame promptStr:(NSString *)str{ UIView *noDataView = [[UIView alloc]init]; noDataView.frame = frame; UIImageView *nodataImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,32,32)]; [nodataImgView setImage:[UIImage imageNamed:@"icon_no_data"]]; nodataImgView.center = CGPointMake(noDataView.center.x, noDataView.center.y-30); [noDataView addSubview:nodataImgView]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,300,25)]; label.textAlignment = NSTextAlignmentCenter; label.center = CGPointMake(noDataView.center.x,noDataView.center.y); label.font = [UIFont systemFontOfSize:NoDataFontOfSize]; label.text = str; label.numberOfLines = 2; label.textColor = [UIColor lightGrayColor]; [noDataView addSubview:label]; return noDataView; } @end