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

查看:152
本文介绍了从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

任何人都可以提供帮助我觉得我可能在创建和加载xibs的自定义uiviews方面犯了一个错误。

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一样,然后将它们连接起来打开Utilities选项卡并从那里控制拖动。

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

错误。文件所有者应为空.T他认为自己是文件所有者。这意味着您应该在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:@ContactUsViewowner:self options:nil]

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

...

self =(ContactUsView *)object;

self = (ContactUsView *)object;

传递 self 后as 所有者参数。你改变它。这意味着以前分配的 ContactUsView self )将被销毁,因为 -loadNibNamed:owner:选项:不保留它。如果您申请我的第一个建议,您应该发送 nil 作为所有者参数

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 loop这里没有必要只使用 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天全站免登陆