码迷,mamicode.com
首页 > 其他好文 > 详细

自定义UIView

时间:2014-07-18 10:15:11      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:strong   os   io   for   re   c   

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);

自定义UIView,布布扣,bubuko.com

自定义UIView

标签:strong   os   io   for   re   c   

原文地址:http://www.cnblogs.com/xubojoy/p/3852469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!