开发者

Using an IBOutlet across all classes

开发者 https://www.devze.com 2023-03-29 22:12 出处:网络
I\'ve consulted a similar thread to the following code that I have so far. Unfortunately, this doesn\'t work as planned. The following code produces no error/warning but will do n开发者_如何学JAVAothi

I've consulted a similar thread to the following code that I have so far. Unfortunately, this doesn't work as planned. The following code produces no error/warning but will do n开发者_如何学JAVAothing. I want the IBOutlet class level because it will be used in a class level method as stated below.

#import <Foundation/Foundation.h>

@interface Interface : NSObject <NSApplicationDelegate> {NSTextView* textView;}

@property (nonatomic, retain) IBOutlet NSTextView* textView;

+ (void)NSPrint:(NSString*)aString;

@end

meanwhile in Interface.m..

 #import "Interface.h"

@implementation Interface
@synthesize textView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [Interface NSPrint:@"Have fun with this program!"];
}
+ (void)NSPrint:(NSString*)aString
{
    [[[[[Interface new] textView] textStorage] mutableString] appendFormat:@"%@\n",aString];
}


I'm not really sure what you're doing here. NSPrint creates a instance of Interface, and changes a property of one of its instance variables. Then the method ends.

What are you expecting to observe from this? You don't have the instance of Interface created in NSPrint: connected to anything or doing anything- you just create it and leave it floating free. To actually access the instance of Interface created by [Interface new], use something like:

+(Interface*)NSPrint:(NSString*)aString
{
      Interface* newInterface = [Interface new];
      [[[[newInterface textView] textStorage] mutableString] appendFormat:@"%@\n",aString];
      return newInterface;
}

On the subject of IBOutlet: IBOutlet has no meaning except to alert XCode's interface builder that it should allows you to create connections between a controller object's variable (marked with IBOutlet) and another object created in the interface builder. Then, when the view is loaded, the controller object's variable's setter method is called using the connected object as the argument. You end up with an instance of the controller class with its instance variable set to an object created in the interface Builder.

For more info on that, look in the Objective-C manual on the Apple website: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html or at the information about interface builder: http://developer.apple.com/library/mac/#recipes/xcode_help-interface_builder/_index.html#//apple_ref/doc/uid/TP40009971

If you need a class variable (that is, a variable connected to a class and not to a specific instance of that class), you have a problem. Take a look at this question for more on that: Objective C Static Class Level variables

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号