假定咱们即将要写的项目是一个tabbar+navgation的项目结构。html
选择Single ViewApplication,命名为HomeHome
删除选中的三项。git
1.删除info.plist main.storyboard字段github
2.添加字段
info.plist 中添加如下
Bundle display name --- 对应应用名
开启http访问,不添加该字段致使不能访问http,只能访问https。web
3.添加应用白名单
iOS9以后分享等必须添加白名单后才能够打开相关应用。字段值为LSApplicationQueriesSchemesxcode
我在这里主要依赖了如下库:浏览器
AFNetWorking 网络请求
SDWebImage 图片加载
MWPhotoBrowser 图片浏览器
MJRefresh 刷新加载控件
RDVTabBarController 一个很好用的tabbar控件
MBProgressHUD 菊花转圈
NJKWebViewProgress webview的加载进度条
Masonry 用于适配微信
一般集成了:网络
均根据官方文档安装ide
该声明文件用于查询接口等
该声明文件用于编写某些全局配置参数
如如下:
#define kPushPhotoBrowserNotifitationName @"PushPhotoBrowser" #define kPresentVideoPlayerNotifitationName @"playCallBackVideo" #define APPICONIMAGE [UIImage imageNamed:[[[[NSBundle mainBundle] infoDictionary] valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject]] #define APPNAME [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"] #define Main_Color [UIColor colorWithRed:(3)/255.0 green:(160)/255.0 blue:(235)/255.0 alpha:1.0] #define Main2_Color [UIColor colorWithRed:(135)/255.0 green:(202)/255.0 blue:(231)/255.0 alpha:1.0] #define VTColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] #define Text_Color [UIColor colorWithRed:(51)/255.0 green:(71)/255.0 blue:(113)/255.0 alpha:1.0] #define BackGround_Color [UIColor colorWithRed:(235)/255.0 green:(235)/255.0 blue:(241)/255.0 alpha:1.0] #define Default_Person_Image [UIImage imageNamed:@"default_parents"] #define Default_General_Image [UIImage imageNamed:@"default_general"] #define kScreenW [UIScreen mainScreen].bounds.size.width #define kScreenH [UIScreen mainScreen].bounds.size.height //以及各类第三方服务商的appId或者App key
咱们分别创建了3个AppDelegate的类别
HHAppDelegate+AppService //app的服务管理
HHAppDelegate+AppLifeCircle//app的生命周期管理
HHAppDelegate+RootController//app的跟视图控制器实例
先看看HHAppDelegate+RootController
声明文件
/** * 首次启动轮播图 */ - (void)createLoadingScrollView; /** * tabbar实例 */ - (void)setTabbarController; /** * window实例 */ - (void)setAppWindows; /** * 设置根视图 */ - (void)setRootViewController;
实现:
- (void)setRoot { UINavigationController * navc = [[UINavigationController alloc] initWithRootViewController:self.viewController]; navc.navigationBar.barTintColor = Main_Color; navc.navigationBar.shadowImage = [[UIImage alloc] init]; [navc.navigationBar setTranslucent:NO]; [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; [navc.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}]; navc.navigationBar.tintColor = [UIColor whiteColor]; self.window.rootViewController = navc; } - (void)setTabbarController { HomePageViewController *school = [[HomePageViewController alloc]init]; AboutChildViewController *child = [[AboutChildViewController alloc]init]; CommuntiyViewController *edu = [[CommuntiyViewController alloc]init]; SZCourseListViewController *courseList = [[SZCourseListViewController alloc]init]; AboutMeViewController *about = [[AboutMeViewController alloc]init]; RDVTabBarController *tabBarController = [[RDVTabBarController alloc] init]; [tabBarController setViewControllers:@[school,edu,child,courseList,about]]; self.viewController = tabBarController; tabBarController.delegate = self; [self customizeTabBarForController:tabBarController]; } - (void)setAppWindows { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; [[UIApplication sharedApplication]setStatusBarHidden:NO]; [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent]; [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}]; } - (void)createLoadingScrollView { //引导页实例 }
注意:这里将Navgation做为根视图控制器,tabbar做为Navgation的rootViewController。
如此全局只有一个跟导航控制器。页面Controller与Tabbar处于平级状态。这与接下来咱们弹出各类页面均由关联
自此一个大体的结构就出来了,一个Tabbar+Navgation的格式
这类信息大都跟需求有关,咱们在这里处理项目相关信息。如可能会由如下信息
- (void)registerBugly; /** * 基本配置 */ - (void)configurationLaunchUserOption; /** * 友盟注册 */ - (void)registerUmeng; /** * Mob注册 */ - (void)registerMob; /** * 检查更新 */ - (void)checkAppUpDataWithshowOption:(BOOL)showOption; /** * 上传用户设备信息 */ - (void)upLoadMessageAboutUser; /** * 检查黑名单用户 */ -(void)checkBlack;
以上咱们处理了跟视图,服务,app声明周期等方法实现
如今咱们开始调用这些方法。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self setAppWindows]; [self setTabbarController]; [self setRootViewController]; [self configurationLaunchUserOption]; [self registerBugly]; [self registerMob]; [self registerUmeng]; [VTJpushTools setupWithOptions:launchOptions]; [self upLoadMessageAboutUser]; [self checkAppUpDataWithshowOption:NO]; [SZNotificationCenter addObserver:self selector:@selector(phontoBroserPush:) name:kPushPhotoBrowserNotifitationName object:nil]; [SZNotificationCenter addObserver:self selector:@selector(playCallBackVideo:) name:kPresentVideoPlayerNotifitationName object:nil]; [self.window makeKeyAndVisible]; return YES; }
好了,咱们着重说一下这里两个通知。
分别是收到图片查看的通知、视频播放的通知
这里以查看图片举列。咱们认为查看图片是push到一个新页面中去。
所以咱们须要找到导航控制器
根据以前所说,当前结构中只有一个导航控制器就是Root
+ (UINavigationController *)rootNavigationController { HHAppDelegate *app = (HHAppDelegate *)[UIApplication sharedApplication].delegate; return (UINavigationController *)app.window.rootViewController; }
找到控制器后就变得简单的多了。
咱们开始实现:
这里咱们对传来的数据格式由必定要求,相似于这样的一个格式。
@{@"imageInfo":@[HHImageInfo*info,HHImageInfo *info2],@"index":@3};
缘由:图片通常有高清图片和非高清图片两种格式。以及图片有可能带有文字描述。所以引入ImageInfo。
- (void)phontoBroserPush:(NSNotification *)note { self.imageArr = [NSMutableArray arrayWithCapacity:0]; [self.imageArr removeAllObjects]; NSDictionary *dic = note.object; NSArray *arr = dic[@"imageInfo"]; NSInteger index = [dic[@"index"] integerValue]; self.imageIndex = index; for(HHImageInfo *info in arr) { //[self.imageArr addObject:info.url]; MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:info.url]]; photo.caption = info.desc; [self.imageArr addObject:photo]; } MWPhotoBrowser *browser = [[MWPhotoBrowser alloc]initWithDelegate:self]; browser.zoomPhotosToFill = YES; browser.displayNavArrows = YES; browser.displayActionButton = NO; browser.alwaysShowControls = NO; browser.autoPlayOnAppear = YES; [browser setCurrentPhotoIndex:index]; [[HHAppDelegate rootNavigationController]pushViewController:browser animated:YES]; }
到这里,其余开发人员在进行模块开发的时候须要图片浏览器的时候直接发送通知便可集成。
HHImageInfo中
@property (nonatomic,strong)NSString *desc; @property (nonatomic,strong)NSString *url; //@property (nonatomic,assign)NSInteger imageIndex; + (NSString *)getHUDImageUrl:(NSString *)smallImageUrl;
实现:
+ (NSString *)getHUDImageUrl:(NSString *)smallImageUrl { NSMutableString *str = [NSMutableString stringWithString:smallImageUrl]; NSRange substr = [str rangeOfString:@"_thu"]; while (substr.location != NSNotFound) { [str replaceCharactersInRange:substr withString:@""]; substr = [str rangeOfString:@"_thu"]; } [str insertString:@"" atIndex:0]; return str; }
我一般在RootViewController实现如下方法以供调用
/** * 显示没有数据页面 */ -(void)showNoDataImage; /** * 移除无数据页面 */ -(void)removeNoDataImage; /** * 须要登陆 */ - (void)showShouldLoginPoint; /** * 加载视图 */ - (void)showLoadingAnimation; /** * 中止加载 */ - (void)stopLoadingAnimation; /** * 分享页面 * * @param url url * @param title 标题 */ - (void)shareUrl:(NSString *)url andTitle:(NSString *)title; - (void)goLogin; /** * 状态栏 */ - (void)initStatusBar; - (void)showStatusBarWithTitle:(NSString *)title; - (void)changeStatusBarTitle:(NSString *)title; - (void)hiddenStatusBar;