【问题标题】:creating a custom UIVIew with a NIB file without a view controller使用没有视图控制器的 NIB 文件创建自定义 UIVIew
【发布时间】:2013-01-11 16:24:24
【问题描述】:

我为此苦苦挣扎了几个小时: 我想创建一个从 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
}

【问题讨论】:

    标签: objective-c uiview xib nib


    【解决方案1】:

    试试这个

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

    【讨论】:

      【解决方案2】:

      要在您的应用程序中使用 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];
      
      【解决方案3】:

      这是我使用自定义类文件实现的。我试图将工具栏附加到键盘上,当我创建连接到 .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;
      
       }
      

      【讨论】:

        猜你喜欢
        • 2012-06-05
        • 1970-01-01
        • 2015-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多