Xcode - 一个nib,两个类 - 第二类插座没有连接? [英] Xcode - one nib, two classes - outlets in second class not connecting?

查看:145
本文介绍了Xcode - 一个nib,两个类 - 第二类插座没有连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单一的nib文件,并希望将一些元素连接到一个类中的方法(这是AppDelegate)和一些到另一个类(它只是一个NSObject)。所以模式看起来像这样



buttonOne - > [mainClass set]
buttonTwo - > [auxCalss set]
textFieldOne - > mainClass.field
textFieldTwo - > auxClass.field



两个[set]动作连接到两个类并且工作正常,但只有textField一个连接到mainclass也是AppDelegate。



TextFieldTwo不工作(从auxClass中的[field setStringValue:]调用)auxClass的实例在mainClass applicationDidFinishLaunching方法中分配。 / p>

我认为这是一个问题 - 运行时无法将一些元素连接到auxClass,因为它还不存在,但是当我对一个方法[auxClass set:],它显示auxClass.field连接到非NULL以外的东西。



通常我会看到这是一个坏的设计模式最简单的例子 - 除了一个标签视图,其中两个标签功能在两个不同的类。



后面有一些nib加载魔法

解决方案

我不需要alloc / init一个新的拷贝,我需要在实例化auxClass时执行AppDelegate auxClass中的代码;它已经为你在笔尖做了。只需在mainClass中创建一个IBOutlet,并在Interface Builder中将其链接到nib中的auxClass对象。请参阅此最近的SO问题


I've got a single nib file and want to connect some elements to methods in one class (which is the AppDelegate) and some to another class (which is just an NSObject). So the schema looks like this

buttonOne -> [mainClass set] buttonTwo -> [auxCalss set] textFieldOne -> mainClass.field textFieldTwo -> auxClass.field

The two [set] actions are connected to the two classes and work fine, but only textField one is connected to mainclass, which is also the AppDelegate.

TextFieldTwo doesn't work (from a [field setStringValue:] call in auxClass. An instance of auxClass is allocated in the mainClass applicationDidFinishLaunching method.

I thought this was the problem - that the runtime cannot connect some of the elements to auxClass because it didn't exist yet. But when I put a break on a method in [auxClass set:], it showed that auxClass.field was connected to something other than NULL.

Generally I'd see this as a bad design pattern in anything more than the simplest example - except for something like a tab view where two tabs functionality is in two different classes.

Is there some "nib loading magic" that goes on behind the scenes of the AppDelegate that I need to perform when I instantiate the auxClass?

解决方案

You shouldn't alloc/init a new copy of auxClass in code; it's already done for you in the nib. Just make an IBOutlet in mainClass and hook it up in Interface Builder to the auxClass object in the nib. See this recent SO question.

这篇关于Xcode - 一个nib,两个类 - 第二类插座没有连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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