使用没有视图控制器的NIB文件创建自定义UIVIew

时间:2013-01-28 10:41:31

标签: objective-c uiview xib nib

我正在为此争吵几个小时: 我想创建一个从NIB文件加载的自定义UIView。这就是我所做的:

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

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

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

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

    self = [nib objectAtIndex:0];

4)使用这个自定义View:使用IB,我在我的主ViewController中创建了一个UIView。在自定义类I的视图属性中放置“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
}

3 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分:1)

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

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

-2)你给每个组件一个TAG号码(例如: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];

答案 2 :(得分: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;

 }
相关问题