将自定义子视图(在 xib 中创建)添加到视图控制器的视图 - 我做错了什么

发布于 2024-10-25 03:14:25 字数 1019 浏览 6 评论 0原文

我在 xib 中创建了一个视图(带有活动指示器、进度视图和标签)。 然后我创建了 .h/.m 文件:

#import <UIKit/UIKit.h>

@interface MyCustomView : UIView {
    IBOutlet UIActivityIndicatorView *actIndicator;
    IBOutlet UIProgressView *progressBar;
    IBOutlet UILabel *statusMsg;
}

@end

#import "MyCustomView.h"

@implementation MyCustomView    

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

@end

设置为 MyCustomView 并将 IBOutlet 连接到文件的所有者

在 MyViewController.m 中,我:

- (void)viewDidLoad {

    [super viewDidLoad];   

    UIView *subView = [[MyCustomView alloc] initWithFrame:myTableView.frame];
    [subView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];

    [myTableView addSubview:subView];
    [subView release];
}

在 IB 中,我将文件的所有者和视图标识 我运行应用程序,添加了视图,但看不到标签、进度条和活动指示器。

我做错了什么?

I've created a view in a xib (with an activity indicator, a progress view and a label).
Then I've created .h/.m files:

#import <UIKit/UIKit.h>

@interface MyCustomView : UIView {
    IBOutlet UIActivityIndicatorView *actIndicator;
    IBOutlet UIProgressView *progressBar;
    IBOutlet UILabel *statusMsg;
}

@end

#import "MyCustomView.h"

@implementation MyCustomView    

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

@end

In IB, I set the file's owner and view identity to MyCustomView and connect the IBOutlet to the File's owner

In MyViewController.m, I've:

- (void)viewDidLoad {

    [super viewDidLoad];   

    UIView *subView = [[MyCustomView alloc] initWithFrame:myTableView.frame];
    [subView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];

    [myTableView addSubview:subView];
    [subView release];
}

When I run the app, the view is added, but I can't see the label, the progress bar and the activity indicator.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三岁铭 2024-11-01 03:14:25

您需要使用 -loadNibNamed 方法加载它。 -initWithNibName 仅适用于 UIViewController。

将以下代码添加到您的 MyCustomView init 方法中:

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
[self addSubview:mainView];

记住,如果您从笔尖初始化一个对象,它会调用 - (id)initWithCoder:(NSCoder *)aDecoder 进行初始化,因此您将如果您要在笔尖中创建 MyCustomView 对象,则可以覆盖它。如果您只是使用 initWithFrame: 执行此操作,则只需覆盖它并添加上面的代码即可。另外,在你的 nib 中,确保你有一个顶级 UIView,并将所有其他元素放置在其中(这确保你的 subviewArray 只有一个条目)。

这将从笔尖加载视图并将它们添加到对象中,并且应该可以解决问题。

You need to load it using the -loadNibNamed method. -initWithNibName is only for UIViewControllers.

Add the following code to your MyCustomView init method:

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
[self addSubview:mainView];

Remember, if you are initializing an object from a nib, it calls - (id)initWithCoder:(NSCoder *)aDecoder to initialize, so you'll have to override that if you are creating the MyCustomView object within the nib. If you're just doing it with initWithFrame:, then just override that and add the code above. Also, in your nib, make sure you have one top-level UIView, and place all other elements within that (that makes sure that your subviewArray only has one entry).

This will load the views from the nib and add them to the object, and should do the trick.

对岸观火 2024-11-01 03:14:25

我认为你需要使用这种方法:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;

这是因为你需要将“nibNameOrNil”中的.xib文件名传递给它。

I think you need to use this method:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;

This is because you need to pass it the .xib filename in the "nibNameOrNil".

愚人国度 2024-11-01 03:14:25

正如霍克所说,您必须使用该方法传递 XIB 名称(不带扩展名)。

然后你必须控制这个列表:

  • 在IB中打开.xib文件
  • 单击文件所有者并选择正确的类(在你的情况下是MyCustomView)
  • 按住control并从文件所有者拖动到视图(现在视图的出口就可以了) )

希望它有效。干杯。

As hocker said you have to use that method passing in the XIB name (without the extension).

Then you have to control this list:

  • Open the .xib file in IB
  • Click on File Owner and select the correct class (MyCustomView in your case)
  • Hold down control and drag from File Owner to the View (Now the outlet for the view is ok)

Hope it works. Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文