简体   繁体   English

用于UIView的iOS initWithNibName

[英]iOS initWithNibName for UIView

Is it possible to implement initWithNubName for a custom class that extends UIView. 是否可以为扩展UIView的自定义类实现initWithNubName。

Example: 例:

.h 。H

@interface UIPullerView : UIView
{
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;

.m .M

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    NSBundle *bundle = nibBundleOrNil==nil ? [NSBundle mainBundle] : nibBundleOrNil;
    NSString *nibName = nibNameOrNil==nil ? @"UIPullerView" : nibNameOrNil;
    if (self == [[bundle loadNibNamed:nibName owner:self options:nil] objectAtIndex:0]) 
    {
        NSLog(@"yes, the same class");
    }
    return self;
}

some controller class calls this 一些控制器类调用它

UIPullerView *puller = [[UIPullerView alloc] initWithNibName:nil bundle:nil];

This is not a working example because I don't set the first XIB object to self. 这不是一个工作示例,因为我没有将第一个XIB对象设置为self。 If I would I would loos a reference to the main class right? 如果我愿意放弃对主类的引用吗? So I would like to add a method that would read XIB file and replicate the object from XIB into self. 所以我想添加一个读取XIB文件并将对象从XIB复制到self的方法。 I don't want to use UIViewController but the UIView so no additional views should be added to main view. 我不想使用UIViewController而是使用UIView,因此不应在主视图中添加其他视图。

Se also how I set the XIB: 另外我也是如何设置XIB的:

在此输入图像描述

I would do 我会做

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
...
    self = [[bundle loadNibNamed:nibName owner:self options:nil] objectAtIndex:0]
    return self;
}

but this isn't right true? 但这不对吗?

Is that possible? 那可能吗?

To get around the issue of assigning self and losing the main class reference, I wonder if you should do it similarly as with loading a UITableViewCell from a NIB: 为了解决分配self和丢失主类引用的问题,我想知道你是否应该像从NIB加载UITableViewCell那样做:

  [[NSBundle mainBundle] loadNibNamed:@"MyTableCell" owner:self options:nil];
  cell = myTableCell;
  self.myTableCell = nil;

and set the class that is doing this as the owner of the NIB. 并将正在执行此操作的类设置为NIB的所有者。

Of course, if you're instantiating your UIPullerView in multiple, different, places then that last part gets tricky... 当然,如果你在多个不同的地方实例化你的UIPullerView ,那么最后一部分会变得棘手......

Create a ExampleView.xib 创建一个ExampleView.xib

Create ExampleView.h and ExampleView.m 创建ExampleView.hExampleView.m

 @interface ExampleView : UIView

 @end

In the ExampleView.xib 's Identity Inspector, set Custom Class to ExampleView ExampleView.xib的Identity Inspector中,将Custom Class设置为ExampleView

在此输入图像描述

In Example.m , add awakeFromNib Example.m ,添加awakeFromNib

- (void)awakeFromNib
{
    [super awakeFromNib];
 // [self whateverInits];
}

which will be executed whenever the view is loaded, say, by: 这将在加载视图时执行,例如:

NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"ExampleView" owner:self options:nil];
ExampleView *exampleView = [objects firstObject];

Try this: 试试这个:

- (instancetype)initWithNib {
    NSString *nibName = [NSString stringWithUTF8String:object_getClassName(self)];
    return [[[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil] objectAtIndex:0];
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM