【问题标题】:Why are my UIView layer properties not being set when called within initWIthCoder:(NSCoder *)aDecoder?为什么在 initWIthCoder:(NSCoder *)aDecoder 中调用时没有设置我的 UIView 图层属性?
【发布时间】:2011-07-07 11:39:53
【问题描述】:

我有一个自定义的 View 类,它是 UIView 的子类。

这个视图是通过 ViewController 中的一个 nib 文件加载的:

[[NSBundle main] loadNibNamed:@"MyCustomView" owner:self options:nil];

MyCustomView.h 内部(在 IB 中正确连接为 Main Views 类) 我链接了一些子视图属性:

@interface MyCustomView : UIView <UITextFieldDelegate> {
....
@public

UIView *backgroundLayer; // a subview of the Main View inside the nib
....
}

@property (nonatomic, retain) IBOutlet UIView *backgroundLayer;

此插座已在 Interface Builder 中正确连接。

MyCustomView.m 我有以下实现:

#import <quartzcore/QuartzCore.h>

@implementation MyCustomView

@synthesize backgroundLayer;

- (id)initWithCoder:(NSCoder *)aDecoder {
   self = [super initWithCoder:aDecoder];
   if (self) {
      ....
      self.backgroundLayer.layer.cornerRadius = 12.0f;
      self.backgroundLayer.layer.borderColor = [UIColor lightGrayColor].CGColor;
      self.backgroundLayer.layer.maskToBounds = YES;
      ....
   }

NONE 正在应用这些 backgroundLayer.layer 设置。当我在模拟器中运行时,自定义视图在没有任何这些模块的情况下完全显示在 NIB 中?我错过了什么?我是不是在错误的地方拨打电话?

【问题讨论】:

  • 经过进一步检查,当我试图在 initWithCoder 中对其进行修改时,似乎 backgroundLayer 仍然为 NULL。
  • 更新我们可以删除问题吗?刚刚发现-(void)awakeFromNib

标签: iphone cocoa-touch ios4 uiview interface-builder


【解决方案1】:

这个问题的正确答案是使用方法:

 -(void)awakeFromNib {
    ......
 }

【讨论】:

  • 如果我在没有 nib 的情况下手动编码 UIView,解决方案是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多