1

我在基于故事板的 iOS 应用程序中遇到了一些基础知识。

我喜欢加载一些在 NIB 文件 (xib) 中构建的 UIView。如果我加载视图,initWithCoder将调用 - 方法。

现在我喜欢加载“绿色”视图;我读到,我可以在其中放置一些其他初始化initWithCoder- 但只有“新”对象,什么都没有,关于视图本身,比如 self.backgroundColor 导致内存(可能对象没有完全初始化)观点)。

在视图类本身中setBackgroundColor添加一些东西的最佳位置在哪里?setCornerRadius

4

1 回答 1

0

我这样做是这样的:

NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:nil options:nil];
self = [nibViews objectAtIndex: 0]; //here it would be best practice to loop through nibViews and get the first view that is member of your class. 

此外,nib 文件中的视图设置为 aMyCustomView而不是UIView. 在这两行之后,您可以设置所需的任何值。但是请注意,如果您将框架设置为 super 例如,它将被 xib 文件中的值覆盖。所以最好在加载 nib 之后设置所有内容,而不是之前。

据我所知,UIView加载viewDidLoad时没有回调UIViewController

希望这可以帮助。

干杯!

编辑:

你可以这样做:

- (id)initWithInfo:(MyInfoClass *) selectedInfo Body:(NSString *) _body
{
    if ((self = [super init]))
    {
         NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"MyCustomClass" owner:nil options:nil];
         self = [nibViews objectAtIndex: 0];

         [self setInfo:selectedInfo];
         [self setBody:_body];
    }
}

然后就用那个initWithInfo: Body:方法。

于 2012-10-31T08:02:23.433 回答