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

查看:82
本文介绍了在另一个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做同样的事情? (当然我仍然在代码中设置值,但我想要的位置的标签/ imageView和所有其余的设置在xib)

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我有每个subviews的插座,我在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
我将该视图连接到类中的 myTextView 插件 ButtonTestViewController.m ButtonTest

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 ?

最后我想在中有6个视图ButtonTestViewController.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.

Edit2

Edit2

好吧,我做了你说的一切,它的工作方式像一个魅力。
现在我唯一的问题是,当 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 视图大小。
这是我做的(使用代码解决模糊问题,取自这里 a>)

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;

另一个问题我仍然无法解决。

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。

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中,单击文件的所有者,并将类设置为MyView。这将使其IBOutlets /操作从您的类出现在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) p>

Next, add the following code so that it gets called when MyView is initialized (remember, if you initialize from a nib you'll get inited from - (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中,并且由于您只有一个

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中设计UIViews(和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并链接所有IBOutlet和IBActions

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

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

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