iOS 使用nib文件(一)

本文介绍了iOS中加载nib文件的方法,重点讨论了加载过程、对象初始化及连接恢复。通过加载nib文件,可以将用户界面对象引入代码。返回值数组不包含File's Owner或代理对象,需手动retain防止对象过早释放。常用在自定义cell的xib关联自定义类时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先是加载nib文件的方法:

- (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options;

Return Value

An array containing the top-level objects in the nib file. The array does not contain references to the File’s Owner or any proxy objects; it contains only those objects that were instantiated when the nib file was unarchived. You should retain either the returned array or the objects it contains manually to prevent the nib file objects from being released prematurely. 

Discussion

You can use this method to load user interfaces and make the objects available to your code. During the loading process, this method unarchives each object, initializes it, sets its properties to their configured values, and reestablishes any connections to other objects. (To establish outlet connections, this method uses the setValue:forKey: method, which may cause the object in the outlet to be retained automatically.) For detailed information about the nib-loading process, see Resource Programming Guide.

该方法的返回值是一个数组,数组不包含对File‘s Owner或者任何代理对象的引用,仅仅包含了nib文件在unarchived时被初始化的对象。你应该retain这个数组或者数组中对象以避免对象被提前释放。

你可以使用该方法来加载UI,并使UI中包含的对象在你的代码中是可用的。在加载过程中,该方法unarchived每一个对象,初始化、设置属性被计算出来的值,同时重新建立和其他对象建立的connections。为了建立outlet connections,该方法使用了setValue:forKey:,可能会导致对象被自动retain.

nib文件就是存储了归档对象数据的文件,通过对该类型文件进行解档,我们可以恢复被归档的对象。

#import <UIKit/UIKit.h>

@interface TestNibView : UIView

@property (weak, nonatomic) IBOutlet UIButton *button;
+(instancetype)testNibView;

@end

#import "TestNibView.h"

@implementation TestNibView

+(instancetype)testNibView
{
    return [[[NSBundle mainBundle]loadNibNamed:@"TestNibView" owner:self options:nil] firstObject];
}


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        DLog(@"%@",self.button);
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    DLog(@"%@",self.button);
}

@end
<span style="font-family: Arial, Helvetica, sans-serif;">+(instancetype)testNibView;//只能使用代码创建</span>
 

TestNibView.xib的内容如下图:

为什么这种方法只能通过代码创建,而不能像其他的控件一样,拖到其他的view上呢,下次再说。

上面这种方法在我们使用xib自定义cell,为cell关联自定类时最常用。

对于UITableViewCell和UICollectionViewCell,我们可以直接使用xib生成cell。

     <pre name="code" class="objc">UIXXCell * cell <span style="font-family: Arial, Helvetica, sans-serif;"> =  [[[NSBundle mainBundle]loadNibNamed:@"CellPractice" owner:self options:nil] firstObject];</span>
 

- (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options;中options参数在文档中也有说明,只是很少用到。

在TestNibView.m中还有几个方法,是下次要研究的内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值