4

我为此苦苦挣扎了几个小时:我想创建一个从 NIB 文件加载的自定义 UIView。这就是我所做的:

1)我创建了一个名为“MyView”的类和一个NIB文件。

2)我把类作为文件的所有者自定义类。

3)为了加载NIB文件,我知道我需要这段代码,但我不知道把它放在哪里。我把它放在“init”方法中。

    NSArray * nib = [[NSBundle mainBundle]
                     loadNibNamed: @"MyView"
                     owner: self
                     options: nil];

    self = [nib objectAtIndex:0];

4)使用这个自定义视图:使用 IB,我在我的主 ViewController 中创建了一个 UIView。在自定义类的视图属性中,我放置了“MyView”。我为此视图创建了一个名为“myView”的 IBOutlet,并将其连接到 ViewController 类。

5)现在我不确定是否需要调用“init”或者它是否自动完成,因为它在 NIB 文件中。我尝试了他们两个。无论如何,“myView”类类型被识别为 UIView 而不是 MyView,并且正在显示一个空视图。

我究竟做错了什么?它有另一种方法吗?

谢谢,尼姆罗德


编辑:我根据这里的答案更改了我的代码。这里的所有答案实际上都没有使用自定义类,这就是这里的全部想法。这就是我所做的,但是应用程序崩溃了,因为它认为 testView 是 UIView 而不是 TestView。请帮忙。

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,retain) TestView *testView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray* views=[[NSBundle mainBundle] loadNibNamed:@"TestView" owner:nil options:nil];
    self.testView= [views objectAtIndex:0];
    [self.view addSubview:self.testView]; 
    [self.testView trace]; // calling a method from the custom class
}
4

3 回答 3

4

尝试这个

if ((self = [super initWithCoder:aDecoder])) {
        if ((self = [super initWithCoder:aDecoder])) {
           NSArray * nib = [[NSBundle mainBundle]
                     loadNibNamed: @"MyView"
                     owner: self
                     options: nil];
          [self addSubview:nib[0]];
        }
        return self;
    }
于 2013-01-28T10:47:47.793 回答
1

要在您的应用程序中使用 nib 文件,我建议您这样做,这样更容易

-1) 使用您需要的所有组件创建 Nib 文件,例如它包含一个 UILabel 和一个 UIButton。

-2)你给每个组件一个标签号(例如:100,101)

-3)在您的代码中,无论是在 init 中还是在 viewController 中的 Methode 中,您都可以这样称呼它:

NSArray* views=[[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:nil options:nil];
    // PS: change "MyCustomView" to your nib file name "MyView" for instance
    // and do not add the .xib extension 
UIView* myCustomView= [views objectAtIndex:0];
// always parse the retrieved components so that you could use them in a proper way
UILabel*    myLabel=(UILabel*)      [myCustomView viewWithTag:100];
UIButton*   myButton= (UIButton*)      [myCustomView viewWithTag:101];
//if you are calling this from a UIView Class
 [self addSubview:mainView]; // add the customview to the main view
//if you are calling this from a ViewController uncomment this line below
//[self.view addSubview:mainView];

完成,现在您可以与自定义视图交互,您可以通过以下方式更改标签文本: myLabel.text=@"someText";

您可以向按钮添加操作:

[myButton addTarget:self action:@selector(someAction) forControlEvents:UIControlEventTouchUpInside];
于 2013-01-28T12:30:05.910 回答
0

这是我使用自定义类文件来实现的。我试图将工具栏附加到键盘上,当我创建连接到 .h 和 .m 文件的单独 XIB 文件时......所以在 .m 文件中,我在“initWithFrame”方法中使用了您的代码,如下所示:

 - (id)initWithFrame:(CGRect)frame
 {

      NSArray * nib = [[NSBundle mainBundle]
                 loadNibNamed: @"MyView"
                 owner: self
                 options: nil];

      self = [nib objectAtIndex:0];

      return self;

 }
于 2013-11-12T13:51:53.223 回答