简体   繁体   English

设置为NSTextField的值被忽略

[英]Value set to NSTextField ignored

I have a class RandomGenerator: 我有一个类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

That when constructed it sets automatically the value of the NSTextField. 构造后会自动设置NSTextField的值。 I allocate a RandomGenerator object from the file GuessTheNumberAppDelegate.m : 我从文件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

And I have made connections in interface builder: 我已经在界面生成器中建立了连接:

连接

界面构建器屏幕

But the content of the NSTextField is not changed, it appears the same, why this? 但是NSTextField的内容没有改变,看起来还是一样,为什么呢?

结果

In -[RandomGenerator init] , you're creating a new text field object that has no relation to the text field that's already in your xib file, and pointing the outlet at that new object. -[RandomGenerator init] ,您将创建一个新的文本字段对象,该对象与xib文件中已有的文本字段没有任何关系,并将出口指向该新对象。 The objects in the xib are real, actual objects that are allocated for you by the loading mechanism. xib中的对象是加载机制为您分配的真实的实际对象。 You don't need textField = [[NSTextField alloc] init]; 您不需要textField = [[NSTextField alloc] init]; ,* nor do you need RandomGenerator* random=[[RandomGenerator alloc]init]; ,*也不需要RandomGenerator* random=[[RandomGenerator alloc]init]; . Both of those objects already exist in your xib. 这些对象都已经存在于您的xib中。

You do need to change a few things, however. 但是,您确实需要更改一些内容。 First, if you want your application delegate to be able to access the RandomGenerator , you'll need to give it an outlet and connect it: IBOutlet RandomGenerator * generator; 首先,如果您希望您的应用程序代表能够访问RandomGenerator ,则需要为其提供一个插座并进行连接: IBOutlet RandomGenerator * generator; . Second, you will need to move [textField setStringValue:@"Insert a number between 1 and 100"]; 其次,您需要移动[textField setStringValue:@"Insert a number between 1 and 100"]; out of -[RandomGenerator init] . -[RandomGenerator init] Due to the way nib loading works, the generator's init method will be called before the IBOutlet to the text field is hooked up, and possibly before the text field is even created. 由于笔尖加载的工作方式,将在连接到文本字段的IBOutlet之前,甚至可能在创建文本字段之前,调用生成器的init方法。

I'm pretty sure that if you add: 我很确定,如果您添加:

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

to RandomGenerator , that will do the trick. RandomGenerator ,就可以解决问题。 Once the nib has been loaded and all the objects in it are recreated, awakeFromNib should be sent to all those objects. 笔尖加载完毕并重新创建其中所有对象后,应将awakeFromNib发送给所有这些对象。


*and that's not the correct initializer for an NSTextField anyways *而且那不是NSTextField的正确初始化器

I agree with Josh, especially on the awakeFromNib part. 我同意Josh的观点,尤其是在awakeFromNib部分。 Below are a couple of additional notes/tests, I coded this up just to check it out. 以下是一些其他注释/测试,我将其编码以进行检查。 Below are the RandomGenerator files but simplified to illustrate what I thought your question was about: 以下是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

And then the AppDelegate files: 然后是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

Building and running the project I get what you might expect: 构建并运行项目,我得到了您期望的结果:

在此处输入图片说明

with

在此处输入图片说明

Note that I didn't need to connect the RandomGenerator as an IBOutlet, I simply made sure its header was included in the GuessTheNumberAppDelegate.h file. 请注意,我不需要将RandomGenerator作为IBOutlet连接,只需确保其标头包含在GuessTheNumberAppDelegate.h文件中即可。 But keep in mind that Josh may have something more general in mind, so you may still need to do that. 但是请记住,Josh可能有一些更一般的想法,因此您可能仍然需要这样做。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM