美文网首页
控制器加载方式

控制器加载方式

作者: 阿浩ah | 来源:发表于2017-05-15 11:34 被阅读61次

    写在前面的话

    近期接到这样一个需求,需要为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

    相关文章

      网友评论

          本文标题:控制器加载方式

          本文链接:https://www.haomeiwen.com/subject/nbocxxtx.html