在另一个 xib 中使用 xib 对象 [英] Using xib object inside another xib

查看:15
本文介绍了在另一个 xib 中使用 xib 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 IB 设计一个特定的按钮(带有一些标签,以及 imageView 和其他东西).

I'm designing using IB a specific button (with some labels,and imageView and other stuff).

然后我想在另一个 xib 中使用该 xib 对象(按钮),我有该按钮的 6 个对象.

I would like then to use that xib object (the button) in another xib, where I have 6 objects of that button.

我知道我可以使用类标识符以编程方式做到这一点,但是我必须定位我的标签和图像视图,并在代码中设置默认值和其他所有内容.

I know I can do that programmatically using the Class Identifier, but then I have to position my lables and image view, and set the default values and everything else in code.

我想知道是否可以仅使用 IB 来做同样的事情?(当然我仍然会在代码中设置值,但我想在 xib 中设置标签/imageView 的位置以及所有其余部分)

I'm wondering if it's possible to do the same thing using just the IB ? (of course I would still set the values in code, but I want to positions of the lables/imageView and all the rest to be set in the xib)

谢谢

编辑

所以,我知道我最初要求的内容是不可能的.我现在正在尝试的是这样的:

So, I understand what I asked for at first is not possible. What I'm trying now is like that :

我创建了一个 ButtonTest.xib.在 Xib 内部,我有一个 UIView 和 3 个子视图(2 个标签和一个按钮).在检查器中,我将 UIView 类设置为 ButtonTest.在 ButtonTest.m 中,每个子视图都有 3 个出口,我在 IB 中连接它们.

I created a ButtonTest.xib. Inside the Xib I have a UIView and 3 subviews (2 lables and a button). In the inspector I set the UIView class to ButtonTest. Inside ButtonTest.m I have 3 outlets for each of the subviews, which I connect in IB.

接下来我有一个 ButtonTestViewController.xib.在其中我放置了一个视图并将其在检查器中的类设置为 ButtonTest.我将该视图连接到 ButtonTestViewController.mButtonTest

Next I have a ButtonTestViewController.xib. Inside it I put one view and set it's class in the inspector to be ButtonTest. I connect that view to a myTextView outlet inside ButtonTestViewController.m of class ButtonTest

现在,这是 ButtonTestViewController.m viewDidLoad 中的代码:

Now, this is the code inside ButtonTestViewController.m viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"ButtonTest" owner:nil options:nil];
    ButtonTest *mainView = (ButtonTest*)[subviewArray objectAtIndex:0];

    self.myTextView = mainView;
}

我希望会发生的是,ButtonTestViewController.xib 中的视图会变成我在 ButtonTest.xib 中设计的视图.这只是没有发生.发生的情况是 ButtonTestViewController.xib 中的视图保持不变.

What I hoped would happen, is that the view in ButtonTestViewController.xib would become the view I designed in ButtonTest.xib. This just isn't happening. what happens is that the view inside ButtonTestViewController.xib stays the same.

值得一提的是,如果我添加:

Worth mentioning, is that if I add:

[self.view addSubview:mainView];

除了现有视图之外,它确实添加了新视图.

It does add the new view, besides the existing one.

知道如何做我想做的事吗?

Any idea how to do what I want ?

最终我希望在 ButtonTestViewController.xib 中有 6 个视图,所有视图看起来都像 ButtonTest.xib 模板,并且标签值将在代码中设置.

Eventually I would like to have 6 views in ButtonTestViewController.xib, all would look like the ButtonTest.xib template, and the lables values will be set in code.

编辑2

好吧,伙计们,我照你说的做了,而且效果很好.我现在遇到的唯一问题是 ButtonTestViewController.xib 中的视图比 ButtonTest.xib 中的视图大一点.发生这种情况时,按钮上的标签看起来非常模糊.当它们的大小相同时,一切都很好.

Ok, guys I did everything you said and it worked like a charm. The only problem I have right now with this issue is when the view in ButtonTestViewController.xib is a little bigger then view in ButtonTest.xib. When that happens, The lable on the button look extremely blurry. When they are both the same size, it's all good.

@ Ned - 我使用了您在 InitWithCoder 方法中发布的确切代码,但我将框架语句切换为:

@ Ned - I used the exact code you posted in my InitWithCoder method, except I switched the frame sentence to this :

self.bounds = mainView.frame;

我尝试在 IB 中使用内容模式尝试修复它,但无济于事.

I tried playing with the content mode in IB trying to fix it, to no avail.

当我像你发布的那样使用它时,意思是:

When I do use it like you posted, meaning :

mainView.frame = self.bounds;

它根本不起作用,两个视图的大小保持不变.在这里我尝试使用 resizeMask 但仍然没有工作.

It's not working at all, and both views' sizes stay the same. Here I tried playing with the resizeMask but still didn't work.

想知道如何解决这两个问题?谢谢大家!

And idea how to fix these 2 issues ? Thanks guys!

编辑 3

我确实设法解决了其中一个问题,将 ButtonTestViewController.xib 调整为 ButtonTest.xib 视图大小.这就是我所做的(使用代码解决模糊问题,取自 here)

I did manage to fix one of the issues, resizing the ButtonTestViewController.xib to the ButtonTest.xib view size. This is what I did (using code to solve the blurry issue, taken from here)

self.bounds = CGRectMake(0, 0, mainView.frame.size.width, mainView.frame.size.height);
        CGRect overlay2Frame = self.frame;
        overlay2Frame.origin.x = round(overlay2Frame.origin.x);
        overlay2Frame.origin.y = round(overlay2Frame.origin.y);
        self.frame = overlay2Frame;

另一个我仍然无法解决的问题.ButtonTest.xib 中的视图不会变得更大以匹配其他视图.

The other issue I still can't solve. the view in ButtonTest.xib just won't get bigger to match the other view.

推荐答案

你可以做到这一点,而且非常容易.我这样做的方法是创建一个 UIView 子类(假设它称为MyView.m"和MyView.h"),然后创建一个名为MyView.xib"的 nib.

You can do this, and pretty easily. The way I do this is create a UIView subclass (let's say it's called "MyView.m" and "MyView.h"), and then a nib called "MyView.xib".

首先,在 nib 中,单击 File's Owner,然后将类设置为MyView".这将使您班级中的 IBOutlets/Actions 出现在 IB 中.添加一个顶级视图(如果还没有)作为主视图,并将其他自定义元素(UILabels 和 UIImageViews)添加为主 UIView 的子视图.

First, in the nib, click File's Owner, and set the class to "MyView". This will make it so IBOutlets/Actions from your class show up in IB. Add a single top-level view (if it doesn't have one already) as your main view, and add your other custom elements (UILabels and UIImageViews) as subviews of the main UIView.

接下来,添加以下代码,以便在 MyView 初始化时调用它(请记住,如果您从 nib 初始化,它将通过 - (id)initWithCoder:(NSCoder *)aDecoder 进行初始化).

Next, add the following code so that it gets called when MyView is initialized (remember, if you initialize from a nib it'll get initialized via - (id)initWithCoder:(NSCoder *)aDecoder).

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];

//Just in case the size is different (you may or may not want this)
mainView.frame = self.bounds;

[self addSubview:mainView];

它的作用是将视图层次结构从您的 nib 加载到 NSArray 中,并且由于您只有一个顶级 UIView,您只需将其添加为自定义 UIView 的子视图.

What this does is loads the view hierarchy from your nib into the NSArray, and since you only had one top-level UIView, you just add that as a subview of your custom UIView.

这允许您在 Interface Builder 中设计 UIView(以及 UIView 的其他子类).

This allows you to design UIViews (and other subclasses of UIView) in Interface Builder.

更新为 OP 的编辑.

Updated to OP's edit.

我一直这样做的方式是首先按照上面的指示进行操作.一个区别是,在您的 ButtonTest 笔尖中,您将 UIView 的类更改为 ButtonTest,但我将文件所有者的类更改为 ButtonTest.然后,在 ButtonTest.m 中,执行 loadNibNamed 的内容.现在,要将该对象添加到 ButtonTestViewController.xib,添加一个 UIView(或 UIButton,无论 ButtonTest 是从哪个子类中继承的)并将类更改为 ButtonTest.

The way I've always done this is by first following my directions above. One difference is that in your ButtonTest nib you're changing the class of the UIView to ButtonTest, but I change the File Owner's class to ButtonTest. Then, inside ButtonTest.m, do the loadNibNamed stuff. Now, to add that object to ButtonTestViewController.xib, add a UIView (or UIButton, whatever ButtonTest is subclassed from) and change the class to ButtonTest.

这有点混乱,所以我会尝试按文件分解.

This is a little confusing, so I'll try to break it up by file.

ButtonTestViewController.xib:添加一个 UIButton(任何 ButtonTest 继承自)并将类更改为 ButtonTest

ButtonTestViewController.xib: Add a UIButton (of whatever ButtonTest inherits from) and change the class to ButtonTest

ButtonTest.m:在initWithCoder"方法中,执行我上面的所有loadNibNamed"操作

ButtonTest.m: Inside the "initWithCoder" method, do all of the "loadNibNamed" stuff I have above

ButtonTest.xib:将文件的 Owner 类更改为 ButtonTest 并链接所有 IBOutlets 和 IBActions

ButtonTest.xib: Change File's Owner class to ButtonTest and link up all IBOutlets and IBActions

这篇关于在另一个 xib 中使用 xib 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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