设置为NSTextField的值被忽略

时间:2012-02-26 19:51:18

标签: objective-c interface-builder

我有一个类RandomGenerator:

// 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

2 个答案:

答案 0 :(得分:1)

-[RandomGenerator init]中,您正在创建一个 new 文本字段对象,该对象与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"];的{​​{1}} 。由于nib加载的工作方式,生成器的-[RandomGenerator init]方法将在文本字段的init被连接之前被调用,并且可能在文本字段被创建之前被调用。

我很确定如果你添加:

IBOutlet

- (void)awakeFromNib { [textField setStringValue:@"Insert a number between 1 and 100"]; } ,就可以了。一旦加载了nib并重新创建了其中的所有对象,就应该将RandomGenerator发送给所有这些对象。


*并且这不是awakeFromNib的正确初始化程序

答案 1 :(得分: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文件中。但请记住,Josh可能会考虑更为一般的内容,因此您可能仍需要这样做。

希望这有帮助!

相关问题