带有使用 NIB 的委托的 UIView 子类 [英] UIView Subclass with a delegate using NIB

查看:19
本文介绍了带有使用 NIB 的委托的 UIView 子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用笔尖子类化 UIView.使用以下代码:

<预><代码>- (void)awakeFromNib{[超级awakeFromNib];NSArray *v = [[NSBundle mainBundle] loadNibNamed:@"Qus_​​Scale1to7View" owner:self options:nil];[self addSubview:[v objectAtIndex:0]];}- (id)initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];如果(自己){NSArray *v = [[NSBundle mainBundle] loadNibNamed:@"Qus_​​Scale1to7View" owner:self options:nil];[self addSubview:[v objectAtIndex:0]];}回归自我;}

这会正确创建对象并显示视图,当对象从其笔尖加载时,委托立即变为 null 并忽略任何为其分配值的尝试.

谁能知道这是什么原因?

提前致谢.

解决方案

对于多个视图控制器重用相同的 xib 是行不通的.如果您想重用该视图,请创建一个从 UIView 继承的类并在那里添加代码.

#import "SomeProtocol.h"@interface MyCustomView : UIView {IBOutlet UIView *headerView;IBOutlet UIView *footerView;IBOutlet UIButton *updateBtn;}@property (nonatomic,assign) id代表;@结尾………………@implementation BCFirmwareView@synthesize 委托 = _delegate;+ (id)viewFromNibWithName: (NSString*)name {UIView *view = nil;NSArray *views = [[NSBundle mainBundle] loadNibNamed: name owner: self options: nil];如果(意见){for (UIView *aView in views) {if ([aView isKindOfClass: NSClassFromString(name)])视图 = aView;}}返回视图;}- (id)initWithCoder:(NSCoder *)aDecoder {self = [super initWithCoder: aDecoder];如果(自我){}回归自我;}- (id)init {self = [[MyCustomView viewFromNibWithName: @"MyCustomView"] 保留];如果(自我){}回归自我;}- (void)dealloc {self.delegate = nil;[headerView 发布];[footerView 发布];[updateBtn 发布];[超级解除分配];}- (void)awakeFromNib {[超级awakeFromNib];//从其笔尖加载视图后进行任何其他设置.headerView.backgroundColor = [UIColor redColor];footerView.backgroundColor = [UIColor greenColor];}- (void)willMoveToSuperview:(UIView *)newSuperview {[超级 willMoveToSuperview: newSuperview];如果 (!newSuperview)返回;}- (void)didMoveToSuperview {[超级 didMoveToSuperview];}- (IBAction)updateBtnPressed: (id)sender {//做一些事情}@结尾

下一步是在 Interface Builder 中打开 xib 并将您的类设置为视图的自定义类,而不是文件的响应者.右键单击视图并建立插座和动作连接.

现在您应该能够在任何视图控制器中简单地创建 MyCustomView 的实例并使用它.如果您不忘记将视图自定义类更改为您的类,也可以在 Interface Builder 中工作.

I am trying to subclass a UIView using the nib. Using the following code:


- (void)awakeFromNib
{
    [super awakeFromNib];
    NSArray *v = [[NSBundle mainBundle] loadNibNamed:@"Qus_Scale1to7View" owner:self options:nil];
    [self addSubview:[v objectAtIndex:0]];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        NSArray *v = [[NSBundle mainBundle] loadNibNamed:@"Qus_Scale1to7View" owner:self options:nil];
        [self addSubview:[v objectAtIndex:0]];
    }
    return self;
}

this creates the object correctly and view also displayed and when the object loads from its nib the delegate instantly becomes null and ignores any attempt to assign values to it.

Can anyone know the reason why it is?

Thanks in advance.

解决方案

It won't work reusing same xib for multiple view controllers. If you want to reuse that view make a class that inherits from UIView and add the code there.

#import "SomeProtocol.h"

@interface MyCustomView : UIView {
    IBOutlet UIView *headerView;
    IBOutlet UIView *footerView;
    IBOutlet UIButton *updateBtn;
}

@property (nonatomic, assign) id<SomeProtocol> delegate;

@end

............

@implementation BCFirmwareView

@synthesize delegate = _delegate;

+ (id)viewFromNibWithName: (NSString*)name {
    UIView *view = nil;
    NSArray *views = [[NSBundle mainBundle] loadNibNamed: name owner: self options: nil];
    if (views) {
        for (UIView *aView in views) {
            if ([aView isKindOfClass: NSClassFromString(name)])
                view = aView;
        }
    }

    return view;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder: aDecoder];

    if (self) {
    }

    return self;
}

- (id)init {
    self = [[MyCustomView viewFromNibWithName: @"MyCustomView"] retain];
    if (self) {
    }

    return self;
}

- (void)dealloc {
    self.delegate = nil;

    [headerView release];
    [footerView release];
    [updateBtn release];

    [super dealloc];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Do any additional setup after loading the view from its nib.

    headerView.backgroundColor = [UIColor redColor];
    footerView.backgroundColor = [UIColor greenColor];
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview: newSuperview];

    if (!newSuperview)
        return;
}

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

- (IBAction)updateBtnPressed: (id)sender {
// do some stuff
}

@end

The next step is to open the xib in Interface Builder and set your class as the custom class for the view, not for the File's Responder. Right click on the view and make the outlet and actions connections.

Now you should be able to simply make instances of your MyCustomView in any view controller and use it. Will work from Interface Builder as well if you don't forget to change your view custom class to your class.

这篇关于带有使用 NIB 的委托的 UIView 子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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