Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在没有UIViewController的情况下加载XIB文件_Objective C_Xcode_Uiview_Interface Builder_Xib - Fatal编程技术网

Objective c 在没有UIViewController的情况下加载XIB文件

Objective c 在没有UIViewController的情况下加载XIB文件,objective-c,xcode,uiview,interface-builder,xib,Objective C,Xcode,Uiview,Interface Builder,Xib,我想使用Interface Builder设计一个UIView和一些子视图(UIWebView、UIToolbar、一些UIBarButtonims、一个进度指示器等等),但我认为传统上没有必要这样做,使用UIViewController,使用presentViewController:animated等 因此,我创建了一个自定义类,使用.h文件代码如下: @interface FileInteractionManager : NSObject { } @property (nonatomi

我想使用Interface Builder设计一个UIView和一些子视图(UIWebView、UIToolbar、一些UIBarButtonims、一个进度指示器等等),但我认为传统上没有必要这样做,使用UIViewController,使用
presentViewController:animated

因此,我创建了一个自定义类,使用
.h
文件代码如下:

@interface FileInteractionManager : NSObject {

}

@property (nonatomic, retain) IBOutlet UIView *fileView;
@property (nonatomic, retain) IBOutlet UIWebView *fileWebView;

@property (nonatomic, retain) IBOutlet UIBarButtonItem *printButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *optionsButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *doneButton;
@implementation FileInteractionManager

@synthesize fileView, fileWebView, doneButton, optionsButton, printButton;

-(id)init {

    NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FileInteractionView" owner:self options:nil];

    NSLog(@"Load success!");

    return self;
}
我的.m文件如下所示:

@interface FileInteractionManager : NSObject {

}

@property (nonatomic, retain) IBOutlet UIView *fileView;
@property (nonatomic, retain) IBOutlet UIWebView *fileWebView;

@property (nonatomic, retain) IBOutlet UIBarButtonItem *printButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *optionsButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *doneButton;
@implementation FileInteractionManager

@synthesize fileView, fileWebView, doneButton, optionsButton, printButton;

-(id)init {

    NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FileInteractionView" owner:self options:nil];

    NSLog(@"Load success!");

    return self;
}
最后,我创建了一个名为“FileInteractionView.xib”的独立xib文件,将文件的所有者更改为我在上面创建的自定义类,并连接IBOutlet

当我在类上调用
init
方法时,我可以在调试器中看到所有IBOutlet对象都被正确实例化

我的问题是:

  • loadNibNamed:owner:options:
    方法是加载我的单机.xib文件的正确方法吗?我不喜欢这个方法返回一个我不使用的数组(返回的顶级对象与我的变量
    fileView
    ,但我已经通过Interface Builder链接了它们)

  • 我解决问题的一般方法正确吗?我执行上述步骤是因为我想要一个简单的
    UIView
    对象,我可以将它添加到现有的UIViewController中,而不是显示和关闭一个全新的UIViewController


  • 我使用了一种稍微不同的方法。我创建了UIView的一个子类(MyCustomView,即),然后使用视图的UI创建了xib,并更改了刚刚定义的(主)视图类。在xib中,您可以将出口链接到自定义视图本身(而不是文件所有者)。 最后,在类定义中,我创建了如下函数:

    + (id) newFromNib
    {
        NSArray *nibArray = [[UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil] instantiateWithOwner:nil options:nil];
        return nibArray[0];
    }
    
    请注意: 1) 这是一个类方法,您可以仅对“NSStringFromClass([self class])之类的内容使用“self”,但实际对象是返回的变量 2) 本例假设xib具有相同的类名称(通过NSStringFromClass([self class]),因此我可以复制粘贴它而不做任何更改;),并且您的视图是xib(标准)中定义的第一个视图。如果在一个xib中存储了多个“主”视图,请选择正确的元素

    因此,在需要MyCustomView的地方,我会执行以下操作:

    MyCustomView* mycv = [MyCustomView newFromNib];
    
    然后设置帧/中心并添加到superview。。。
    我认为,如果你有一个复杂UI元素的“库”,并希望通过xib设计它们,然后在需要时添加,那么这种方法是非常有用的。

    il Malvagio Dottor Prosciutto答案很好。这里有一个可能的替代方案

    在NS_指定的_初始值设定项中加载nib并成为子视图的所有者 如果我们接受
    xib
    只保存子视图而不是视图本身,那么我们可以在
    initWithFrame:
    中加载子视图,并在
    xib
    中保留所有权结构

    @interface MyCustomView ()
    @property (strong, nonatomic) IBOutlet UIView *subview;
    @end
    
    @implementation MyCustomView
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
        [self addSubview:self.subview];
        return self;
    }
    @end
    

    这是你应该使用的方法我不确定我是否完全理解答案。如何将IBOutlet链接到自定义视图,而不首先将类分配给文件的所有者?如果自定义视图有更多子视图,该怎么办?如果将视图类设置为自定义视图,则无需指定文件所有者。InterfaceBUilder足够聪明,可以解析MyCustomView.h以查找IBOutlet和iAction。因此,您可以将子视图出口直接链接到MyCustomView。用户界面可以任意复杂。我只是警告您,如果您在xib中放置多个“主”视图(不是常见的或最佳的实践,但可能您需要它),那么它们将是NIBRARY中的不同元素,因此您必须选择正确的一个。通常只有一个元素,所以它总是元素0。谢谢,这很有帮助。如果要从nib实例化多个UIView对象,这是否也能正常工作?(只是想知道,因为它是一个静态方法,我很快会亲自尝试)是的,通过多次调用“newFromNib”方法,您可以拥有多个实例。