2012-02-26 71 views
0

我有一个类RandomGenerator:值设置为忽略的NSTextField

// RandomGenerator.h 
#import <Foundation/Foundation.h> 
@interface RandomGenerator : NSObject 
{ 
    @private 
    IBOutlet NSTextField* textField; 
    unsigned int number; 
} 
@end 

//RandomGenerator.m 
#import "RandomGenerator.h" 
@implementation RandomGenerator 

- (id)init 
{ 
    self = [super init]; 
    if (self) 
    { 
     textField=[[NSTextField alloc]init]; 
     [textField setStringValue:@"Insert a number between 1 and 100"]; 
     srand((unsigned int)time(NULL)); 
    } 
    return self; 
} 
- (void)dealloc 
{ 
    [super dealloc]; 
} 
@end 

所构建时,它将自动设置的NSTextField的值。 我从文件GuessTheNumberAppDelegate.m分配RandomGenerator对象:

#import "GuessTheNumberAppDelegate.h" 
#import "RandomGenerator.h" 
@implementation GuessTheNumberAppDelegate 
@synthesize window; 
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init]; 
    RandomGenerator* random=[[RandomGenerator alloc]init]; 
    [pool drain]; 
} 
@end 

而且我已在界面生成器连接:

Connection

Interface builder screen

不过的NSTextField的含量没有改变,看起来是一样的,为什么呢?

Result

回答

1

-[RandomGenerator init],你要创建一个没有关系的文本字段已经在您XIB文件文本字段对象,并指着新对象出口。 xib中的对象是真实的,由加载机制为您分配的实际对象。你不需要textField = [[NSTextField alloc] init];,*也不需要RandomGenerator* random=[[RandomGenerator alloc]init];。这两个对象都已经存在于你的xib中。

但是你确实需要改变一些东西。首先,如果您希望应用程序代理能够访问RandomGenerator,则需要给它一个插座并将它连接起来:IBOutlet RandomGenerator * generator;。其次,您需要将[textField setStringValue:@"Insert a number between 1 and 100"];移出-[RandomGenerator init]。由于笔尖加载的方式,发生器的init方法将在文本字段的IBOutlet连接之前被调用,并且可能在文本字段被创建之前被调用。

我敢肯定,如果你增加:

- (void)awakeFromNib { 
    [textField setStringValue:@"Insert a number between 1 and 100"]; 
} 

RandomGenerator,会做的伎俩。一旦装入了笔尖并重新创建了笔尖中的所有对象,则应将awakeFromNib发送给所有这些对象。


*这不是一个NSTextField反正

1

我与Josh同意正确的初始化,特别是在awakeFromNib一部分。下面是一些额外的笔记/测试,我编码这只是为了检查它。下面是RandomGenerator文件,但简单地表示什么,我认为你的问题是关于:

// RandomGenerator.h 
#import <Foundation/Foundation.h> 
@interface RandomGenerator : NSObject { 
    IBOutlet NSTextField *textField; 
} 
@end 
-------------- 
// RandomGenerator.m 
#import "RandomGenerator.h" 
@implementation RandomGenerator 
- (void)awakeFromNib { 
    [textField setStringValue:@"Insert a number between 1 and 100"]; 
} 
@end 

然后在AppDelegate的文件:

// GuessTheNumberAppDelegate.h 
#import <Cocoa/Cocoa.h> 
@interface GuessTheNumberAppDelegate : NSObject <NSApplicationDelegate> 
@property (assign) IBOutlet NSWindow *window; 
@end 
-------------- 
// GuessTheNumberAppDelegate.m 
#import "GuessTheNumberAppDelegate.h" 
#import "RandomGenerator.h" 
@implementation GuessTheNumberAppDelegate 
@synthesize window = _window; 
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
    RandomGenerator *random = [[RandomGenerator alloc] init]; 
    NSLog(@"%@",random); 
} 
@end 

构建和运行项目中,我得到你所期望的东西:

enter image description here

enter image description here

请注意,我不需要将RandomGenerator作为IBOutlet连接,我只是确保它的头包含在GuessTheNumberAppDelegate.h文件中。但请记住,乔希可能有一些更一般的想法,所以你可能仍然需要这样做。

希望这会有所帮助!

+0

这帮助了很多,谢谢。 – 2012-02-26 22:44:04