控制器加载方式

写在前面的话

近期接到这样一个需求,需要为app内机构详情页提供2种不同的布局,效果图如下,

机构详情页的2种布局.png

拿到该需求后,你都有哪些思路?
1、创建2个UIViewController, 界面xib实现,逻辑代码贴贴贴。
2、创建1个UIViewController,纯代码实现。
3、创建1个UIViewController, 不同场景加载不同的storyboard或者xib实现。
我们采取第三种方法实现,那就引出了今天的问题,iOS控制器ViewControlle加载都有几种方式?

代码实现

通过alloc或者new方法实现。

故事板加载

Main.storyboard实现如下截图

故事板加载控制器.png

代码实现部分

#import "AHTestViewController.h"

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
AHTestViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"AHDemo2"];

在跳转到机构详情页时只需按照不同场景加载不同故事板即可.


if (item.organ_style.integerValue==1){
            
        OrganOtherDetailViewController *VC = [[OrganOtherDetailViewController alloc]initWithType:detailone];
        VC.item =self.orgaDataArr[indexPath.section];
         [self.navigationController pushViewController:VC animated:YES];
}else if (item.organ_style.integerValue==2){
        OrganOtherDetailViewController *VC = [[OrganOtherDetailViewController alloc]initWithType:detailtwo];
        VC.item =self.orgaDataArr[indexPath.section];
        [self.navigationController pushViewController:VC animated:YES];
        }


xib实现

新建一xib文件,在xib文件中做如下设置

xib加载控制器.png

代码实现部分

#import "AHTestViewController.h"

 AHTestViewController *vc = [[AHTestViewController alloc]initWithNibName:@"AHTest" bundle:nil];
    

演示代码下载https://github.com/ahao1011/CreatViewsDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,227评论 4 61
  • 其实很巧,在参与这次21天手写的活动中,也是我去保险公司学习的时候,所以每天我都是非常匆忙的赶着写东西,因为一颗心...
    小乔是唐宝娘亲阅读 1,657评论 0 3
  • 我们家老朱,在不熟悉的人面前一定是高C的表现,冷静、分析、逻辑、理性,给人一种距离感。一旦成为了朋友并得到他的认同...
    ChristineWang阅读 4,421评论 0 6
  • 惜若时光 灿若霓裳 清风拂起三千青丝 如北海荡起的波浪 渐渐的 轻轻的 满满的 走过你的身旁 嗅一缕飘散指尖的芳香
    不是百家阅读 937评论 0 0