xcode 用于 UIView 的 iOS initWithNibName

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6874182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:36:25  来源:igfitidea点击:

iOS initWithNibName for UIView

iphoneiosxcodeipaduiview

提问by xpepermint

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. 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. I don't want to use UIViewController but the UIView so no additional views should be added to main view.

这不是一个工作示例,因为我没有将第一个 XIB 对象设置为 self。如果我愿意,我会失去对主类的引用吗?所以我想添加一个方法来读取 XIB 文件并将对象从 XIB 复制到 self. 我不想使用 UIViewController 而是 UIView,因此不应向主视图添加其他视图。

Se also how I set the XIB:

还有我如何设置 XIB:

enter image description here

在此处输入图片说明

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?

那可能吗?

回答by onnoweb

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 UITableViewCellfrom a NIB:

为了解决分配 self 和丢失主类引用的问题,我想知道您是否应该像UITableViewCell从 NIB加载 a 一样执行此操作:

  [[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 UIPullerViewin multiple, different, places then that last part gets tricky...

当然,如果你UIPullerView在多个不同的地方实例化你的,那么最后一部分就会变得棘手......

回答by ohho

Create a ExampleView.xib

创建一个 ExampleView.xib

Create ExampleView.hand 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

enter image description here

在此处输入图片说明

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

回答by Xinyan.Wang

Try this:

尝试这个:

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