您的位置:首页 > 产品设计 > UI/UE

UI部分 — 4

2018-01-18 17:36 120 查看
    - 懒加载

    (1)作用:1用到的时候再加载

             2 全局只会被加载一次

             3 全局都可以使用

    (2)过程:1重写成员变量(数据)的getter方法

             2 在getter方法中判断

               如果为空,加载数据

               如果不为空,就直接返回数据

    - plist文件

    (1)写入文件

        //数组  
   

        NSArray *names =@[@“aaa”,@“bbb”,@“ccc”,@“ddd”];

        BOOL flag = [nameswriteToFile:@"/Users/xy/Desktop/names.plist"atomically:YES];

        //字典

        NSDictionary *persons = @{

                                  @"name" :@“aaa”,

                                  @"age" :@18,

                                  @"height" :@1.88

                                  };

        BOOL flag = [persons writeToFile:@"/Users/xy/Desktop/person.plist" atomically:YES];

        //数组和字典混合

        NSArray *persons = @[

                             @{@"name" :@“aaa”,@"age":@38},

                             @{@"name" :@“bbb”,@"age":@25,@“cf”:@[@“xxx”,@“xy”]}

                             ];

        BOOL flag = [persons writeToFile:@"/Users/xy/Desktop/persons.plist" atomically:YES];

      注意:plist文件名不能为info或Info

     (2)读取文件

         //获取Plist文件的全路径

         NSBundle *bundle = [NSBundlemainBundle]; 

         //加载Plist文件 
     

         NSString *path = [bundlepathForResource:@“name”ofType:@"plist"];

    - 字典转换模型

    (1)模型就是专门用来存放数据的对象,即数据模型

         优点:模型设置数据都是通过它的属性,属性名如果写错了,编译器会马上报错,因此保证了数据的正确性

              使用模型访问属性时,编译器会提供一系列的提示,提高编码效率

         - (NSArray *)dataArr{

             //加载数据

             if (_dataArr ==nil)
{

                 //获取全路径

                 NSString *dataPath = [[NSBundlemainBundle]pathForResource:@“nameData.plist"ofType:nil];

                 self.dataArr =
[NSArrayarrayWithContentsOfFile:dataPath];

                 //创建临时数组

                 NSMutableArray *tempArray = [NSMutableArrayarray];

                 for (NSDictionary *dictin_dataArr)
{

                     //创建name对象

                     XYName *name = [XYNamenameWithDict:dict];

                     //把模型装入数组

                     [tempArrayaddObject:name];

                 }

             self.dataArr =
tempArray;

             }

             return_dataArr;

         }

         //获取数据

         XYName *name =self.dataArr[index];

    (2)字典转模型

               //声明

         - (instancetype)initWithDict:(NSDictionary *)dict;

         + (instancetype)proWithDict:(NSDictionary *)dict;

               //实现

         - (instancetype)initWithDict:(NSDictionary *)dict{

             if (self =
[superinit])
{

                 self.icon =
dict[@"icon"];

                 self.name =
dict[@"name"];

         }

             returnself;

         }

         + (instancetype)proWithDict:(NSDictionary *)dict{

             return [[selfalloc]initWithDict:dict];

         }

    - view的封装

    (1)如果一个view内部的子控件比较多,一般会考虑自定义一个view,把它内部子控件的创建屏蔽起来,不让外界关心

              外界可以传入对应的模型数据给view,view拿到模型数据后给内部的子控件设置对应的数据

       (2)封装控件的基本步骤

               1  在initWithFrame:方法中添加子控件,提供便利构造方法

               2  在layoutSubviews方法中设置子控件的frame(一定要调用super的layoutSubviews)

               3  增加模型属性,在模型属性set方法中设置数据到子控件上

    - 自定义控件

       XYShopView.m

    #import <UIKit/UIKit.h>

    @class XYShop;

    @interface XYShopView : UIView

    //商品模型

    @property (nonatomic, strong) XYShop *shop;

    // 构造方法

    - (instancetype)initWithShop: (XYShop *)shop;

    + (instancetype)shopViewWithShop: (XYShop *)shop;

    @end

    XYShopView.h

    #import "XYShopView.h"

    #import "XYShop.h"

    @interface XYShopView ()

    //图片控件

    @property (nonatomic, weak) UIImageView *iconView;

    //标题控件

    @property (nonatomic, weak) UILabel *titleLabel;

    @end

    @implementation XYShopView

    /*   - (instancetype)init{

           if (self = [super init]) {

               //创建子控件

               [self setUp];

           }

           return self;

        } */

    //注意: 创建对象用init方法和initWithFrame:方法都会调用initWithFrame:方法:

    - (instancetype)initWithFrame:(CGRect)frame{

        if (self =[super initWithFrame:frame])
{

            [self setUp];

        }

        return self;

    }

    - (instancetype)initWithShop:(XYShop *)shop
{

       if (self =
[super init]) {

          //创建子控件

          [self setUp];

          //将数据赋值给子控件

          self.shop =
shop;

       }

       return self;

    }

    + (instancetype)shopViewWithShop:(XYShop *)shop
{

       return [[self alloc] initWithShop:shop];

    }

    //初始化

    - (void)setUp {

       //创建商品的UIImageView对象,设置样式

       UIImageView *iconView = [[UIImageView alloc] init];

       iconView.backgroundColor = [UIColor blueColor];

       [self addSubview:iconView];

       _iconView = iconView;

    

       //创建商品UILabel对象,设置样式

       UILabel *titleLabel = [[UILabel alloc] init];

       titleLabel.backgroundColor = [UIColor yellowColor];

       titleLabel.textAlignment = NSTextAlignmentCenter;

       [self addSubview:titleLabel];

       _titleLabel = titleLabel;

    }

    //布局子控件

       //注意:在这里设置frame

    - (void)layoutSubviews {

       //注意:一定要调用super

       [super layoutSubviews]; 
 

       //获取当前控件的尺寸

       CGFloat width = self.frame.size.width;

       CGFloat height = self.frame.size.height;

       //设置子控件的frame

        self.iconView.frame = CGRectMake(0, 0,
width, width);

        self.titleLabel.frame = CGRectMake(0,
width, width, height - width);

   }

    //set方法:只要外边传数据就会调用

       //作用:设置数据

    - (void)setShop:(XYShop *)shop
{

       _shop = shop;

       // 设置数据

       self.iconView.image =
[UIImage imageNamed:shop.icon];

       self.titleLabel.text =
shop.name;

    }

    @end

    使用自定义控件

    XYShopView *shopView = [XYShopView shopViewWithShop:self.dataArr[index]];

    shopView.frame = CGRectMake(x,
y, width, height);

    [self.shopCarView addSubview:shopView];

                

    - MVC

    (1)MVC的全名是Model
View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范。它是用一种业务逻辑、数据与界面显示分离的方法来组织代码,将众多的业务逻辑聚集到一个部件里面,在需要改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑,达到减少编码的时间。

              V即View视图是指用户看到并与之交互的界面。比如由html元素组成的网页界面,或者软件的客户端界面。MVC的好处之一在于它能为应用程序处理很多不同的视图。在视图中其实没有真正的处理发生,它只是作为一种输出数据并允许用户操纵的方式。

              M即model模型是指模型表示业务规则。在MVC的三个部件中,模型拥有最多的处理任务。被模型返回的数据是中立的,模型与数据格式无关,这样一个模型能为多个视图提供数据,由于应用于模型的代码只需写一次就可以被多个视图重用,所以减少了代码的重复性。

              C即controller控制器是指控制器接受用户的输入并调用模型和视图去完成用户的需求,控制器本身不输出任何东西和做任何处理。它只是接收请求并决定调用哪个模型构件去处理请求,然后再确定用哪个视图来显示返回的数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Objective-C OC