问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/5425715/xcode-one-nib-two-classes-outlets-in-second-class-not-connecting