本文介绍了InterfaceBuilder - UIViewController子类未被识别为子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的iPhone应用程序。

I'm working on a simple iPhone app.

我有1个UINavigationController和2个UIViewController子类。

I have 1 UINavigationController and 2 subclasses of UIViewController.

我有一个MainWindow.xib文件,一个PersonListView.xib和一个PersonDetailView.xib。

I have a MainWindow.xib file, a PersonListView.xib, and a PersonDetailView.xib.

MainWindow.xib有UINavigationController,视图链接到PersonListView.xib文件。

The MainWindow.xib has the UINavigationController with the view linked over to the PersonListView.xib file.

PersonListView.xib将文件所有者列为PersonListViewController类。

The PersonListView.xib has the File's Owner listed as a PersonListViewController class.

我在PersonListView上有一些按钮,以及标签和图像。所有显示都正确。

I have some buttons on the PersonListView, along with labels and images. All displays correctly.

当我尝试将PersonListView上的按钮链接到我的PersonListViewController类时,按钮没有触发....

When I try to link a button on the PersonListView to my PersonListViewController class, the button is not firing....

结果:
*由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'* - [UIViewController viewDetail]:无法识别的选择器发送到实例0x450d50'

The result: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[UIViewController viewDetail]: unrecognized selector sent to instance 0x450d50'

如果我进入调试控制台和打印对象0x450d50,我得到:

If I go into the debug console and print-object 0x450d50, I get:

(gdb)print -object 0x450d50

(gdb)

(gdb) print-object 0x450d50(gdb)

Interface Builder正确地将文件所有者显示为PersonListViewController,为什么我的按钮动作(链接到以某种方式转到超类?

The Interface Builder correctly shows the File's Owner as a PersonListViewController, so why would my button action (linked to the File's Owner) go to the superclass somehow?

EDIT1:好的,似乎正在发生的事情是我没有得到子类的实例,除非在我的MainWindow.xib,我明确地将视图控制器的类设置为我的子类。

Ok, what seems to be happening is that I wasn't getting an instance of the subclass unless, in my MainWindow.xib, I explicitly set the class of the view controller to my subclass.

还有一些我错的东西但是,唱歌,因为我只能通过从主窗口交换NIB文件名来启动UIViewController的任意子类吗?

There's still got to be something I'm missing, though, because surely I should be able to start up arbitrary subclasses of UIViewController just by swapping out the NIB file name from the main window?

EDIT2 :(响应凯文)
这不是我所说的XIB内容。

(Response to Kevin)That's not what I'm saying regarding the contents of a XIB.

我有3个XIB:

MainWindow
PeopleListView
PeopleDetailView

MainWindowPeopleListViewPeopleDetailView

PeopleListView和PeopleDetailView每个都有一个相关的文件所有者,它是一个相应的UIViewController子类。显然,当我从一个页面移动到另一个页面时,我需要将我的事件转到正确的控制器类...但是如果我只能使用默认的UIViewController或一个子类,那将会更加困难吗?

The PeopleListView and PeopleDetailView each have an associated File's Owner that is a corresponding UIViewController subclass. Obviously, I need my events to go to the right controller classes as I move from page to page...but if I can only use a default UIViewController or one subclass, that will be much more difficult?

通过手动设置具有正确root的UINavigationController,我可以在代码中获得正确的效果,但我觉得应该有更好的方法通过InterfaceBuilder。

I can get the correct effect in code by manually setting up the UINavigationController with the correct root, but I feel like there should be a better way through InterfaceBuilder.

EDIT3 :(对肯德尔)
我没有使用标签栏。但是,模型是相同的,我的导航控制器中的视图必须连接到单个子类,这似乎有点icky。

(to Kendall)I'm not using a tab bar. The model is the same, though, that the view in my navigation controller has to be wired to a single subclass, which seems a bit icky.

EDIT4 :(到lostintransit )
PersonListViewController有适当的 - (IBAction)viewDetail;方法。问题是我无法在我的MainWindow.xib中找到一种方法来将我的UINavigationController中的视图标记为最初从PersonListView.xib加载它,并使用PersonListViewController作为视图,这样就不会让用PersonDetailView替换PersonListView。

(to lostintransit)The PersonListViewController has the appropriate - (IBAction) viewDetail; method. The problem is that I can't find a way, in my MainWindow.xib, to flag the view inside my UINavigationController as "initially load this from PersonListView.xib, with a PersonListViewController as the view" that wouldn't make it difficult to replace the PersonListView with a PersonDetailView.

推荐答案

我认为你正在使用标签栏,对吗?

I think you are using a tab bar, correct?

如果是这样的话,请这样考虑一下。必须将MainWindow xib中定义的每个选项卡定义为自定义视图控制器类型,因为这是选项卡栏在加载时将创建的类型。标签栏为加载时定义的每个标签创建视图控制器。

If so, think about it this way. Each tab defined in the MainWindow xib must be defined as the custom view controller type, because that is the type that the tab bar will create when it is loaded. The tab bar creates view controllers for each tab defined when it is loaded.

仅创建任何视图控制器的实例然后加载自定义NIB(这是是您定义的标签栏现在正在做什么),因为您的自定义视图中的连接将无需附加任何内容。

It's not enough to create an instance of just any view controller and then load your custom NIB (which is what the tab bar you have defined is doing right now), because the wiring in your custom view will have nothing to attach to.

因此,请确保每个视图控制器在MainWindow.xib中的每个选项卡下定义(使用IB中的树视图来检查这一点)除了指向您的自定义XIB之外,还设置为正确的自定义UIViewController类型,该自定义XIB必须还要将文件的所有者设置为正确的类型(尽管如此)只有在基本视图之外的文件所有者接线时才重要。

Thus, make sure each view controller defined under each tab in MainWindow.xib (use the tree view in IB to examine this) is set to the right custom UIViewController type in addition to pointing at your custom XIB which must ALSO have the file's owner set to the correct type (though that's only important if you are wiring anything to Files Owner beyond the basic view).

这篇关于InterfaceBuilder - UIViewController子类未被识别为子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:21