使用界面构建器创建可循环视图 [英] Using interface builder to Create Loabable Views

查看:95
本文介绍了使用界面构建器创建可循环视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Nib文件中获取项目的新方法。

以下答案中的示例。

这个新的答案是这个项目的最新变化和问题

我想要创建一个UIView子类,我想为UIView创建一个Interface构建器文件。我如何将文件所有者连接到自身。我希望能够设计视图并让它创建连接,但我不需要视图控制器。

I want to create a UIView subclass and I want to create an Interface builder file for the UIView. How would I connect the files owner to itself. I want to be able to Design the view and have it create the connections but I dont have a need for a view controller.

除了将nib加载到数组中之外我使用nib文件作为视图本身的连接器?

Other than loading the nib into an array can I use the nib file as a connector to the view itself ?

我希望我正确地问这个问题。我将详细说明。

I hope i am Asking this properly. I will elaborate tho.

创建UIViewController我可以选择为它创建一个xib文件。那时我可以将UIViews添加到xib中并进行连接。但是文件所有者似乎没有与其关联的UI编辑器,所以我想创建一个UIView,它将成为另一个视图中的子视图。我真的不想用代码创建它,因为手动布局项目很痛苦。所以我想使用相同的构造来创建一个小的uiview来放入另一个

Creating a UIViewController I am given the choice to create an xib file for it. At which time i can add the UIViews to the xib and those connections would be made. However the files owner does not appear to have a UI Editor associated to it so I want to create a UIView that will become a sub view in another view. I dont really want to create it with code because Its a pain to layout the items manually. So I want to use the same construct to create a small uiview to put into another

编辑:

当我找到另一种更好的加载nib类的方法时,我再次编辑了这个。我认为一些堆栈溢出用户希望看到这一点。

我想找到一种从笔尖加载UIView的优雅方法。并且来自堆栈溢出用户PeyloW的一些信息我已经设法做到这一点。

I wanted to find an elegant way to load a UIView from a nib. and with some info from stack overflow user PeyloW I have managed to do just that.

我将发布我的结果,因为信息将导致解决方案,但这个解决方案对我来说感觉特别优雅。

I am going to post my results as the information will lead to a solution but this solution feels exceptionally elegant to me.


  • 首先,我创建了一个名为TableViewCells.xib的Xib文件


    (名称匹配)因此类名[[self class] description] ==TableViewCells)

  • 接下来我创建了我用于Nib中视图的类。
    类并不重要,只需确保为界面构建器中的类对象选择自定义类

  • 最后我创建了类Ta​​bleViewCells 匹配xib文件名

我的新头文件。

(每个导入的头文件都关联到单个xib文件中的项目。)

My new Header File.
(Each of the header files imported associate to the items in the single xib file.)

#import <Foundation/Foundation.h>
#import "TitleCell.h"
#import "ToggleCell.h"
#import "ButtonCell.h"
#import "TextCell.h"

@interface TableViewCells : NSObject {
    UINib *uiNibHolder;
}

@property (nonatomic, readonly) NSArray *nibArray;

// These are the classes that will be loadable.
- (TitleCell*) titleCell;
- (ToggleCell*) toggleCell;
- (ButtonCell*) buttonCell;
- (TextCell*) textCell;
- (id) loadViewByClass: (id) viewClass;
@end

和类文件。

loadViewByClass:是在nib文件中查找项目的方法。

方便访问器具有要实例化的类对象的正确类型。

注意:我不建议加载一堆视图,加载的视图越多,加载nib文件时必须创建的越多。

loadViewByClass: is the method that finds the item in the nib file.
the convenience accessors have the correct type for the class objects to be instantiated into.
NOTE: I would Not Recommend loading a Bunch of Views into this, the more views you load the more you have to create when you load the nib file.

#import "TableViewCells.h"

@implementation TableViewCells

@synthesize nibArray;

- (void) unloadProperties{
    [uiNibHolder release];
}
- (NSArray *)nibArray{
    return [uiNibHolder instantiateWithOwner:nil options:nil];
}
- (id) init{
    if ((self = [super init]))
    {
        // Im using the class name for the Xib file. 
        // This means it will look in TableViewCells.xib
        // You can change the class name, or pass a Nib name to this class 
        uiNibHolder = [[UINib nibWithNibName:[[self class] description] bundle: [NSBundle mainBundle]] retain];
    }
    return self;
}
- (TitleCell *)titleCell{
    return [self loadViewByClass:[TitleCell class]];
}
- (ToggleCell *)toggleCell{
    return [self loadViewByClass:[ToggleCell class]];
}
- (ButtonCell *)buttonCell{
    return [self loadViewByClass:[ButtonCell class]];
}
- (TextCell *)textCell{
    return [self loadViewByClass:[TextCell class]];
}
- (id)loadViewByClass:(Class)viewClass{
    for (id item in self.nibArray) 
    {
        if ([item isKindOfClass:viewClass])
        {
            return item;
        }
    }
    return nil;
}

- (void)dealloc{
    [self performSelector:@selector(unloadProperties)];
    [super dealloc];
}

@end

能够按班级加载非常好。如果类不在xib文件中,它将返回nil。

Being able to load by class is incredibly nice. And if the class is not in the xib file it will return nil.

这可以适用于使用多个nib文件,具体取决于您加载的视图类型。使用延迟加载属性技术,您可以仅在需要时加载这些笔尖,使内存占用空间很小,并且仍允许您通过一个方便的类加载器来加载类。

This could be adapted to use multiple nib files depending on the type of views you were loading. Using a lazy load property technique you could load those nibs only when needed leaving the memory footprint small and still allowing you to get the classes loaded through one convenient class loader.

As演示我使用TableView加载一小撮视图

As a demonstration I used a TableView to load a handfull of views

@synthesize tableViewCellLoader = _tableViewCellLoader;
- (TableViewCells *)tableViewCellLoader{
    if (_tableViewCellLoader == nil){
        _tableViewCellLoader = [[TableViewCells alloc] init];
    }
    return _tableViewCellLoader;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            return self.tableViewCellLoader.toggleCell;
            break;
        case 1: 
            return self.tableViewCellLoader.textCell;
            break;
        case 2:
            return self.tableViewCellLoader.titleCell;
            break;
        case 3:
            return self.tableViewCellLoader.buttonCell;
            break;
    }
    return nil;
}

如你所见,我懒得加载我的TableViewCellLoader,如果我想,我可以只有在调用类时才使TableViewCells类延迟加载UINib对象。

As you can see I lazy loaded my TableViewCellLoader and if I wanted to I could make the TableViewCells class lazy load UINib objects only when their classes were called.

我喜欢代码中的便利。

仍然,
再次感谢PeyloW。

And Still, Thanks again PeyloW.

推荐答案

文件所有者只是一个代理对象,不需要在运行时指定,但在Interface Builder中的设计时始终可见。

Files Owner is only a proxy object, it is not required to be specified at run-time, but will always be visible at design-time in Interface Builder.

您可以忽略挂钩的任何内容文件所有者如果您不需要它。 at运行时使用以下内容加载NIB文件:

You may ignore hooking anything up to Files Owner if you do not need it. The at run-time load the NIB-file using something like this:

NSArray* rootObject = [[NSBundle mainBundle] loadNibNamed:@"MyNib" 
                                                    owner:nil 
                                                  options:nil];

您为所有者参数传递的内容是加载笔尖时作为文件所有者代理传递的内容, nil 工作正常。

What you pass for the owner argument is what passes as the Files Owner proxy when loading the nib, nil works just fine.

rootObject 数组包含所有根级别对象,但不包含代理。请注意,数组是自动释放的,因此如果您需要保留加载的对象,则必须保留数组,或者只保留您感兴趣的特定元素。

The rootObject array contains all root level objects, but no proxies. Do note that the array is autoreleased, so if you need the loaded objects to stay around you must retain the array, or just the particular elements you are interested in.

不使用 loadNibNamed:owner:options:是IO绑定的。如果需要性能,那么你应该使用 UINib 的实例来缓存内存中的NIB文件并从中实例化对象。

Not that using loadNibNamed:owner:options: is IO bound. If performance is needed then you should use an instance of UINib to cache the NIB file in memory and instantiate objects from it.

UINib* myNib = [[UINib nibWithNibName:@"MyNib" 
                               bundle:[NSBundle mainBundle]] retain];
// And then instantiate using:
NSArray* rootObject = [myNib instantiateWithOwner:nil
                                          options:nil];

这篇关于使用界面构建器创建可循环视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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