UIView和initWithFrame以及一个NIB文件。如何加载NIB文件? [英] UIView and initWithFrame and a NIB file. How can i get the NIB file loaded?

查看:104
本文介绍了UIView和initWithFrame以及一个NIB文件。如何加载NIB文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 baseView UIView ,并且我有 initWithFrame 我在哪里添加一些其他视图并做一些自定义的东西。同样的视图也有一个NIB文件。

I have a UIView called baseViewand in that i have initWithFramewhere i add some other views and do some custom stuff. The same view also has a NIB file.

现在我有一个名为 UIViewController 类AppController 其中我想将 baseView 视图添加到 AppController 视图的视图中所以我这样做:

Now i have a UIViewController class named AppController in which i want to add the baseView view to the view of the AppController view so i am doing this :

self.view = baseView; 但问题是NIB文件没有加载。如何确保定制的东西和NIB文件加载/运行?

self.view = baseView; but the problem is that the NIB file does not get loaded. How do i make sure the customized stuff AND the NIB file get´s loaded/run ?

推荐答案

你有很多选择,具体取决于关于如何使用baseView类并将其集成到应用程序中。目前还不清楚你打算如何使用这个类 - 作为UIViewController子类中的视图,或者作为可重用的模块化组件,意味着在整个应用程序中多次实例化,以便在许多不同的视图控制器中使用。

You have many options, depending on how your "baseView" class is meant to be used and integrated in to your application. It's not clear just how you intend to use this class -- as the view in a UIViewController subclass, or as a reusable modular component mean to be instantiated multiple times throughout your application, for use in many different view controllers.

如果您的视图是UIViewController子类中的唯一视图,那么Phonitive是正确的 - 将它与UIViewController子类.xib文件捆绑在一起,并使用UIViewController的viewDidLoad进行最终初始化。

If your view is meant to be the only view in a UIViewController subclass, then Phonitive is correct -- bundle it together with the UIViewController subclass .xib file and use the UIViewController's viewDidLoad to do final initialization.

但是如果你希望你的View类是在不同视图控制器中多次重复使用的子组件,可以通过代码或通过包含在另一个控制器的.xib文件中进行集成,然后你需要实现initWithFrame:init方法和awakeFromNib来处理这两种情况。如果您的内部初始化总是包含来自.xib的某些对象,那么在您的initWithFrame中,您需要手动加载.xib以支持想要通过代码创建窗口小部件的客户类。同样,如果.xib文件包含您的对象,那么您需要确保从awakeFromNib调用任何代码所需的最终化。

But if you want your View class to be a subcomponent reused multiple times in different view controllers, integrated either via code or via inclusion in a .xib file for another controller, then you need to implement both the initWithFrame: init method, and awakeFromNib, to handle both cases. If your internal initialization always includes some objects from .xib, then in your initWithFrame you'll need to load your .xib manually in order to support "customer" classes that want to create your widget via code. And likewise, if a .xib file contains your object then you'll need to make sure you call any code-required finalization from awakeFromNib.

以下是如何使用的示例使用笔尖中的UI设计创建一个UIView子类组件。

Here's an example of how to create a UIView subclass component with the UI design in a nib.

MyView.h:

@interface MyView : UIView
{
    UIView *view;
    UILabel *l;
}
@property (nonatomic, retain) IBOutlet UIView *view;
@property (nonatomic, retain) IBOutlet UILabel *l;

MyView.m:

#import "MyView.h"
@implementation MyView
@synthesize l, view;

- (id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code.
        //
        [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil];
        [self addSubview:self.view];
    }
    return self;
}

- (void) awakeFromNib
{
    [super awakeFromNib];

    // commenters report the next line causes infinite recursion, so removing it
    // [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil];
    [self addSubview:self.view];
}

- (void) dealloc
{
     [l release];
     [view release];
     [super dealloc];
}

这是nib文件的样子(除了需要更改文件的所有者)到MyView类)。

Here's what the nib file looks like (except that file's owner needs to be changed to MyView class).

请务必将视图和标签出口连接到文件所有者。而已!用于创建可重用的UIView小部件的模板。

be sure to hook up both the view and label outlets to File's Owner. That's it! A template for creating re-usable UIView widgets.

这个结构的真正好处在于你可以将MyView对象的实例放在其他nib文件中,只需放置一个UIView在你想要的位置/大小,然后将身份检查器(CMD-4)中的类更改为MyView,并且繁荣,你有一个你想要的任何视图的小部件实例!就像UIKit对象一样,您可以实现委托协议,以便使用您的窗口小部件的对象可以被通知有趣的事件,并且可以提供要在窗口小部件中显示的数据以进行自定义。

The really neat thing about this structure is that you can place instances of your MyView object in other nib files, just place a UIView at the location/size you want, then change the class in the identity inspector (CMD-4) to MyView, and boom, you've got an instance of your widget in whatever views you want! Just like UIKit objects you can implement delegate protocols so that objects using your widget can be notified of interesting events, and can provide data to display in the widget to customize it.

这篇关于UIView和initWithFrame以及一个NIB文件。如何加载NIB文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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