EchoAppDelegate.h
NSString *theString;
EchoAppDelegate.m
/////being declared somewhere here//////
theString = [lastUserInputJabber stringValue];
ChatController.m
//Get theString variable from echoappdelegate
NSString *theStringDiff = theString;
How would I do this?
EchoAppDelegatemust provide a method that returns that string, or make that string a public ivar. For instance, you could implement a getter method like:and
or make it a declared property and have Objective-C automatically provide a getter method:
and
(Depending on your target/compiler, you may not need to declare the ivar — the modern runtime and recent enough compilers can automatically create backing ivars for declared properties. Also, depending on your design, you might want to make
theStringareadwrite copyproperty, in which case you’ll also get a setter method that copies an arbitrary string ontotheString.)Having done that, your application delegate now exposes a method that returns that string. When you need to access it in an implementation file other than the application delegate one, use
-[NSApplication delegate]to obtain the delegate, and then use the getter method to obtain the string:As jer pointed out, you should ponder whether the application delegate is the right place to keep that string. The application delegate should be concerned with information and behaviour that applies to the entire application.