zoukankan      html  css  js  c++  java
  • 自定义UIView

    Whenever we want to add an instance of this custom view to the visible screen, we now need to:

    1. load the Nib, and
    2. extract the actual view from the array of loaded objects (because we did not connect it to any outlet on the File’s Owner), and finally
    3. add the newly-instantiated custom view as a subview and set its frame.

    Something like this:

    MyCustomView customView = nil; NSArray* elements = [[NSBundle mainBundle] loadNibNamed: @”MyCustomView” // (1) owner: nil options: nil]; for (id anObject in elements) { // (2) if ([anObject isKindOfClass:[MyCustomView class]]) { customView = anObject; break; } } [self.view addSubview:customView]; // (3) customView.frame = CGRectMake(100.0, 100.0, 400.0, 90.0);

    This is tedious because most of it is generic code that is repeated every time we instantiate a custom view from a Nib. Therefore, my colleague Michael moved all boilerplate code to a UIView category, resulting in the following class method:

    // UIView+NibLoading.m

    • (UIView) loadInstanceFromNib { UIView result = nil; NSArray* elements = [[NSBundle mainBundle] loadNibNamed: NSStringFromClass([self class]) owner: nil options: nil]; for (id anObject in elements) { if ([anObject isKindOfClass:[self class]]) { result = anObject; break; } } return result; }

    With this category, the instantiation code becomes much sleeker:

    MyCustomView* customView = [MyCustomView loadInstanceFromNib]; [self.view addSubview:customView]; customView.frame = CGRectMake(100.0, 100.0, 400.0, 90.0);

  • 相关阅读:
    Web框架高级功能之模板、拦截器、Json、打包
    前端技术发展史、Nodejs语法
    子集树与排列树
    Web开发---路由实现
    JS, Jquery进行前台翻页
    Jquery将Ajax返回的Response添加到table中
    显示字符太长??来隐藏
    关于code 上server的证书的问题
    关于Ajax call get and post method, 和前端解决跨域
    2017-05-03与03May2017之间的转化
  • 原文地址:https://www.cnblogs.com/xubojoy/p/3852469.html
Copyright © 2011-2022 走看看