实例变量不保留其值

发布于 2024-12-01 21:35:58 字数 1278 浏览 0 评论 0原文

我现在正在学习 Objective-C,为了练习,我为 OS X 编写了一个简单的随机迷宫生成器,效果很好。接下来,我尝试添加更多与按钮的交互,但我在使用实例变量时遇到了问题,因为它们不保留我分配给它们的值。我遇到过关于同一问题的多个问题,但这些问题的解决方案并没有解决我的问题。我还测试了该程序的简化版本是否仍然存在同样的问题,确实如此。

我想我做错了什么,但我不知道是什么。这就是我所做的:

  • 创建一个新项目
  • 添加一个名为“TestClass”的 NSView 子类
  • 在 MainMenu.xib 的窗口中添加一个带有 TestClass 类
  • 的视图 在 MainMenu.xib 中添加一个 TestClass 对象
  • 在视图中添加一个按钮并设置其标签1
  • 将以下代码添加到TestClass.h和TestClass.m并将按钮连接到它:

TestClass.h: #导入

@interface TestClass : NSView
{
    NSNumber *number;
    NSButton *test;
}

@property (nonatomic, retain) NSNumber *number;


@property (assign) IBOutlet NSButton *test;

- (IBAction)testing:(id)sender;

@end

TestClass.m: #import "TestClass.h"

@implementation TestClass

@synthesize number;
@synthesize test;

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (IBAction)testing:(id)sender
{
    self.number = [[NSNumber numberWithLong:[sender tag]] retain];
}

- (void) drawRect:(NSRect)dirtyRect
{
    NSLog(@"%@", number);
}

@end

每当我按下按钮时,NSLog 都会返回 null 几次。

我通常会自己解决所有问题(最终......),但这一次它真的让我发疯,所以有人可以帮助我吗?

I'm learning Objective-C right now and in order to practice I wrote a simple random maze generator for OS X, which works fine. Next I tried to add some more interaction with buttons, but I'm having trouble with the instance variables as they don't retain the value I assign them. I have come across multiple questions about the same problem, but the solutions to those haven't solved my problem. I also tested if the same problem persists in a simplified version of the program, which it does.

I guess I'm doing something wrong, but I don't know what. Here's what I did:

  • Created a new project
  • Added a subclass of NSView called "TestClass"
  • Added a view with class TestClass in the window in MainMenu.xib
  • Added an object for TestClass in MainMenu.xib
  • Added a button to the view and set its tag to 1
  • Added the following code to TestClass.h and TestClass.m and connected the button to it:

TestClass.h:
#import

@interface TestClass : NSView
{
    NSNumber *number;
    NSButton *test;
}

@property (nonatomic, retain) NSNumber *number;


@property (assign) IBOutlet NSButton *test;

- (IBAction)testing:(id)sender;

@end

TestClass.m:
#import "TestClass.h"

@implementation TestClass

@synthesize number;
@synthesize test;

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (IBAction)testing:(id)sender
{
    self.number = [[NSNumber numberWithLong:[sender tag]] retain];
}

- (void) drawRect:(NSRect)dirtyRect
{
    NSLog(@"%@", number);
}

@end

Whenever I press the button, NSLog just returns null several times.

I normally figure out everything by myself (eventually...), but this time it's really driving me insane, so is there anyone who can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨离汐 2024-12-08 21:35:58

将 NSLog 放入 testing: 中,或者只是在那里放置一个断点,看看 number 中存储了什么。

请注意, self.number = [[NSNumber numberWithLong:[sender tag]] keep]; 是双重保留 NSNumber 对象(这是错误的),但这不会立即导致任何错误。

Put the NSLog in testing:, or just put a breakpoint there and see what's stored in number.

Note that self.number = [[NSNumber numberWithLong:[sender tag]] retain]; is double-retaining the NSNumber object (which is wrong), but that shouldn't cause any immediate error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文