简体   繁体   English

在XIB中连接UIView并使用Story板中的View Controller加载它

[英]Connect UIView in XIB and load it using View Controller in the Story board

I have a XYZViewController (simple UIViewController in storyboard) that is loaded up with the default view. 我有一个XYZViewController (故事板中的简单UIViewController),它加载了默认视图。 I have a type XYZView for which I have UIView in a .xib file. 我有一个XYZView类型,我在.xib文件中有UIView

In the XYZViewController class, I have defined property for XYZView as an IBOutlet . 在XYZViewController类中,我已将XYZView属性定义为IBOutlet What is tricky is I don't know how to connect this property to the UIViewController in storyboard (or UIVIew in .xib file) such that — 什么是棘手的是我不知道如何将这个属性连接到storyboard(或.xib文件中的UIVIew)中的UIViewController,这样 -

  • the IBOutlet is connected to the right UIView IBOutlet连接到右侧UIView
  • the view in the xib becomes an added subview for the default view of the UIViewController. xib中的视图成为UIViewController默认视图的附加子视图。

(I under the question sounds dodgy and/or I may not have the very right way to explain it, but that's the best I could.) (我在这个问题下听起来很狡猾和/或我可能没有正确的解释方法,但这是我能做的最好的。)

EDIT: Further clarification may make it easier. 编辑:进一步澄清可能会使它更容易。 I just don't want to myself say: 我只是不想自己说:

XYZView *xyzView = [[XYZView alloc] initWithFrame...]; 

or 要么

[self.view addSubview:xyzView];

Maybe that helps. 也许这有帮助。

OK, from what I tell you have the following... 好的,据我所说,你有以下内容......

XYZViewController The code of this is in XYZViewController.h and .m files. XYZViewController它的代码在XYZViewController.h and .m文件中。

A .storyboard file In the storyboard file you have a view controller that you have set the subclass to XYZViewController . .storyboard文件在storyboard文件中,您有一个视图控制器,您已将子类设置为XYZViewController

A .xib file In the xib file you have a single view that you have defined as the subclass XYZView . .xib 文件在xib文件中,您有一个已定义为子类XYZView

Right? 对?

I'm guessing what you have done is the following... 我猜你所做的是以下......

In the .xib file you have laid out the XYZView and put labels, buttons, etc... on it. 在.xib文件中,您已经布置了XYZView并在其上放置标签,按钮等。

The view controller is being created by the storyboard. 视图控制器由故事板创建。 But now you want to attach the labels and buttons to it. 但现在您想要将标签和按钮附加到它上面。

Right? 对?

If all this is correct then you have a couple of options. 如果这一切都是正确的那么你有几个选择。

The easiest option 最简单的选择

Drop the xib file. 删除xib文件。 Unless that XYZView is being used in multiple places in the app (ie inside different view controllers) then you should really be doing all of that layout in the storyboard. 除非在应用程序的多个位置使用XYZView (即在不同的视图控制器中),否则您应该在故事板中进行所有这些布局。 Add the buttons and labels to the XYZViewController in the storyboard. 将按钮和标签添加到故事板中的XYZViewController

This will then allow you to connect the IBOutlets and IBActions and it will all just work because the storyboard is creating and then setting the outlets and actions. 这将允许您连接IBOutlets和IBActions,它将全部正常工作,因为故事板正在创建,然后设置插座和操作。

Next option 下一个选项

Because you have created the view in a xib file you have to load it from that xib file in code and then add it to you view controller. 因为您已在xib文件中创建了视图,所以必须在代码中从该xib文件加载它,然后将其添加到视图控制器。

Something like... 就像是...

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.xyzView = [[[NSBundle mainBundle] loadNibNamed:@"XYZView" owner:self options:nil] objectAtIndex:0];

    [self.view addSubview:xyzView];
}

Then you can do stuff like ... 然后你可以做像......

self.xyzView.someLabel.text = @"This is the text";

You still won't be able to connect outlets and actions but that's because the view controller is not being created by the xib. 您仍然无法连接插座和操作,但这是因为xib没有创建视图控制器。 It's being created by the storyboard. 它是由故事板创建的。

This can get all messy though. 这可能会让一切变得混乱。 I'd really only recommend creating a view in a separate xib if it's something that you reuse over and over (like a 5star rating view or something). 我真的只建议在一个单独的xib中创建一个视图,如果它是你一遍又一遍地重复使用的东西(比如5星评级视图或其他东西)。

What you absolutely can't do 你绝对做不到的

OK, I think I may have thought of what you are doing. 好吧,我想我可能已经想到你在做什么了。

In the storyboard you have set the subclass of the view as XYZView and you are expecting it to pick up the labels and buttons etc... that you have defined in the xib file for XYZView . 在故事板中,您已将视图的子类设置为XYZView并且您希望它能够拾取标签和按钮等...您已在xib文件中为XYZView定义。

This absolutely will not work, ever. 这绝对不会奏效。

The storyboard and the xib are completely separate objects. 故事板和xib是完全独立的对象。 If you want to use them together then code is involved in loading a view from a nib and then adding it to a view controller created in a storyboard. 如果要将它们一起使用,则代码涉及从笔尖加载视图,然后将其添加到在故事板中创建的视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM