从 Xib 加载的自定义 UIView [英] Custom UIView loaded from Xib

查看:23
本文介绍了从 Xib 加载的自定义 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了 UIView 的自定义子类以及一个 xib 文件,并在自定义类中声明了 IBOutlets 和 IBActions.

I have created a custom subclass of UIView along with a xib file and declared IBOutlets and IBActions within the custom class.

@interface ContactUsView : UIView

@property (nonatomic, weak) IBOutlet UIButton *displayCloseButton;

- (IBAction)callButtonPressed:(id)sender;
- (IBAction)emailButtonPressed:(id)sender;
- (IBAction)displayCloseButtonPressed:(id)sender;

@end

在 xib 文件中,我拖入了一个 UIView 来表示我的自定义视图.我已经设置:

In the xib file I have dragged in a UIView to represent my custom view. I have set:

  • 文件所有者 = 我的自定义类
  • 已将 UIView 中的拖动设置为我的自定义类.

然后我添加了各种按钮,这些按钮与上述 3 种方法相关联.

I have then added various buttons which are hooked up to the 3 methods stated above.

在 ContactUsView.m 中,我有以下内容:

Inside the ContactUsView.m I have the following:

- (id)initWithFrame:(CGRect)frame 
{
    if (self = [super initWithFrame:frame]) {
        NSArray* array = [[NSBundle mainBundle] loadNibNamed:@"ContactUsView" owner:self options:nil];
        for (id object in array) {
            if ([object isKindOfClass:[ContactUsView class]])
                self = (ContactUsView *)object;
        }
    }
    return self;
}

当我开始创建此视图时,我会执行以下操作:

When I come to create this view I do the following:

- (void)viewWillAppear:(BOOL)animated
{
    ContactUsView *contactUs = [[ContactUsView alloc] initWithFrame:CGRectZero];

    CGPoint origin = self.view.frame.origin;
    CGSize size = self.view.frame.size;
    [contactUs setFrame:CGRectMake(origin.x,
                              CGRectGetMaxY(self.view.frame) - 100,
                              size.width,
                              contactUs.frame.size.height)];

    [self.view addSubview:contactUs];
}

问题当我按下其中一个按钮时,应用程序崩溃:线程 1:EXC_BAD_ACCESS(代码=2,地址=0xb0c

Issue When I press on one of the buttons the application crashes with: Thread 1: EXC_BAD_ACCESS(code=2, address=0xb0c

谁能帮我解决这个问题.我觉得我可能在从 xibs 创建和加载自定义 uiview 的某个地方犯了一个错误.

Can anyone help me with this. I feel like I am probably making a mistake somewhere in regards to creating and loading custom uiviews from xibs.

如果您需要更多信息,请告诉我.非常感谢.

If you require anymore information let me know. Many thanks.

未来参考使用 xib 创建自定义视图时,请勿设置文件所有者.而是像往常一样创建所有 IBOutlets 和 IBActions,然后将它们连接起来,打开实用工具"选项卡并从那里控制拖动.

Future reference When creating a custom view using a xib DO NOT set the files owner. Instead create all your IBOutlets and IBActions as you normally would and then to hook them up open the Utilities tab and control drag from there.

推荐答案

• 文件所有者 = 我的自定义类

• Files owner = to my custom class

错了.文件所有者应为空.视图本身是文件所有者.这意味着您应该在您的 xib 中使用 ContactUsView 连接所有操作和出口.

Wrong. Files owner should be empty. The view itself is files owner. It means that you should connect all actions and outlets with ContactUsView in your xib.

[[NSBundle mainBundle] loadNibNamed:@ContactUsView";owner:self 选项:nil]

[[NSBundle mainBundle] loadNibNamed:@"ContactUsView" owner:self options:nil]

...

self = (ContactUsView *)object;

self = (ContactUsView *)object;

在您将 self 作为 owner 参数传递之后.你改变它.这意味着之前分配的 ContactUsView (self) 将被销毁,因为 -loadNibNamed:owner:options: 不保留它.如果你应用我的第一个建议,你应该发送 nil 作为 owner 参数

After you passed self as ownerparameter. You changing it. Which means that previously allocated ContactUsView (self) will be destroyed since -loadNibNamed:owner:options: do not retain it. If you apply my first advice you should send nil as owner parameter

for 此处的循环不必仅使用 array[0],因为如果您的 xib 中有有效的视图层次结构,这始终是您的视图

forloop here is not necessary use just array[0], because this is always your view if you have valid views hierarchy in your xib

这篇关于从 Xib 加载的自定义 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆