单UIViewcontroller,如何在多个xib之间切换? [英] Single UIViewcontroller, how to switch among multiple xibs?

查看:215
本文介绍了单UIViewcontroller,如何在多个xib之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIViewController的子类

 @interface KBViewController : UIViewController

多个 xib s,用于例如,一个是 Qwerty ,另一个是 Dvorak 布局:

with multiple xibs, for example, one is a Qwerty and the another is Dvorak layout:

 KBViewControllerQuerty~iphone.xib
 KBViewControllerDvorak~iphone.xib

因此,当用户点击按钮时, Qwerty 将切换到 Dvorak 。如您所见,两种键盘布局的代码逻辑都是相同的。我需要的是用另一个 xib 重新加载视图

So when the user click a button, Qwerty is switch to Dvorak. As you may see, the code logic is identical for both keyboard layouts. What I need is to reload the view with another xib.

希望 Dvorak xib 中的所有按钮都会挂钩到响应的 IBOutlet s在 KBViewController

Hopefully, all the buttons in the Dvorak xib will be hooks to the responding IBOutlets in KBViewController.

在两者之间切换的正确方法是什么 XIB S'

What is the right way to switch between the two xibs?

推荐答案

所有Nib都有指定的文件所有者。 IBOutlet和IBAction链接基于文件的所有者完成。所以你可以定义一个视图控制器和两个Nib,每个Nib文件的文件的所有者设置为定义的视图控制器。

All Nibs has the designated File's Owner. IBOutlet and IBAction linking is done based upon the File's Owner. So you could define a view controller and two Nibs, with each Nib file's File's Owner set to the defined view controller.

也就是说,如果你将所有KBViewController * .xib文件的文件所有者设置为KBViewController并在某处有一个KBViewController对象,你可以加载你想要的KBViewController * .xib code> initWithNibNamed 方法(重新创建视图控制器)

That is, if you set File's Owner of all KBViewController*.xib files to KBViewController and have a KBViewController object somewhere you could load the KBViewController*.xib you want by initWithNibNamed method (recreate the view controller)

如果你应该一直保持相同的KBViewController对象,你可以创建一个没有Nib的KBViewController对象。在KBViewController.m中,实现 loadView 并使用 - [NSBundle loadNibNamed] 方法手动加载UIView对象(加载和更改self.view编程)。

If you should maintain the same KBViewController object all along, you could create a KBViewController object without Nib. In KBViewController.m, implement loadView and manually load the UIView object with -[NSBundle loadNibNamed] method (load and change self.view programmatically).

UIView *someView = [[[NSBundle mainBundle] loadNibNamed:@"SomeNibFile"
                                                  owner:self
                                                options:nil] objectAtIndex:0];
self.view = someView;

注意所有者:自我在上面的代码中。它必须与@SomeNib​​File的文件所有者匹配。

Note owner:self in above code. It must match with File's Owner of the @"SomeNibFile".

要更改已加载的视图:

id superview = self.view.superview;
[self.view removeFromSuperview];
UIView *someView = [[[NSBundle mainBundle] loadNibNamed:@"SomeNibFile"
                                                  owner:self
                                                options:nil] objectAtIndex:0];
self.view = someView;
[superview addSubview:self.view];

更详细的解释:
资源编程指南 - 以编程方式加载Nib文件

这篇关于单UIViewcontroller,如何在多个xib之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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