繁体   English   中英

从Xib加载的自定义UIView

[英]Custom UIView loaded from Xib

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

@interface ContactUsView : UIView

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

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

@end

在xib文件中,我拖入UIView来表示我的自定义视图。 我已经设定:

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

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

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

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

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

- (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时犯了一个错误。

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

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

在此输入图像描述

•Files owner =我的自定义类

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

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

...

self =(ContactUsView *)对象;

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

这里的for循环不一定只使用array[0] ,因为如果你的xib中有有效的视图层次结构,这总是你的视图

如果要为xib加载UIView,则应创建一个类方法来加载视图。

在customview.h中

+(id)customView;

在您的customview.m中

+ (id)customView
{
    CustomView *customView = [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil] lastObject];
    if ([customView isKindOfClass:[CustomView class]])
        return customView;
    else
        return nil;
}

您可以使用以下命令将其初始化:

CustomView *myView = [CustomView customView];

编辑:确保您已在身份检查器中更改了customview的类,并确保您的IBActions与该类的方法的连接。 在此输入图像描述

在此输入图像描述

你可以使用委托这就是你如何做到这一点

        @protocol CustomViewDelegate <NSObject>
- (void)callButtonPressed:(id)sender;
- (void)emailButtonPressed:(id)sender;
- (void)displayCloseButtonPressed:(id)sender;

@end

@interface ContactUsView : UIView

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

@property (nonatomic, weak) id<CustomViewDelegate> ButtonDelegate;

- (IBAction)callButtonPressed:(id)sender;

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


@end

并在.m文件中

- (IBAction)callButtonPressed:(id)sender
{
[self.ButtonDelegate callButtonPressed:sender];
}

- (IBAction)emailButtonPressed:(id)sender{
[self.ButtonDelegate emailButtonPressed:sender];
}

- (IBAction)displayCloseButtonPressed:(id)sender{
[self.ButtonDelegate displayCloseButtonPressed:sender];
}

之后,只需使用viewcontroller参考设置委托,并在此处使用这些委托

- (void)viewWillAppear:(BOOL)animated

{

ContactUsView *contactUs = [[ContactUsView alloc] initWithFrame:CGRectZero];
contactUs.ButtonDelegate = self;

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

}

- (void)callButtonPressed:(id)sender

{}

- (void)emailButtonPressed:(id)sender

{}

- (void)displayCloseButtonPressed:(id)sender

{}

我做到了这一点并且工作得非常好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM