【问题标题】:IBOutlet for NSOutlineView doesn't point a valid instanceNSOutlineView 的 IBOutlet 未指向有效实例
【发布时间】:2012-12-13 13:41:00
【问题描述】:

我在 Xcode 中创建了一个新的 Cocoa 应用程序项目,然后将一个 NSOutlineView 和一个 NSTextView 对象添加到窗口中。这两个对象被归类为 MyOutlineView 和 MyTextView。之后我为他们做了两个出口并编写了如下代码。

我发现问题是应用程序在运行时有两个不同的 MyOutlineView 实例。工作(有效)大纲视图实例不等于 myOutlineView 插座实例。我错过了什么?

//
//  AppDelegate.h

#import <Cocoa/Cocoa.h>
#import "MyOutlineView.h"
#import "MyTextView.h"

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet MyOutlineView *myOutlineView;
@property (unsafe_unretained) IBOutlet MyTextView *myTextView;

@end

//
//  AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)n
{
    NSLog(@"AppDelegate.myOutlineView(INVALID)::%@", _myOutlineView);
    NSLog(@"AppDelegate.myTextView::%@", _myTextView);
}

@end

//
//  MyOutlineView.h

#import <Cocoa/Cocoa.h>

@interface MyOutlineView : NSOutlineView <NSOutlineViewDataSource>;

@end

//
//  MyOutlineView.m

#import "MyOutlineView.h"

@implementation MyOutlineView

- (id)initWithCoder:(NSCoder *)aDecoder
{
    // This method is called first.
    self = [super initWithCoder:aDecoder];
    NSLog(@"MyOutlineView initWithCoder(INVALID)::%@", self);
    return self;
}

- (id)initWithFrame:(NSRect)frame
{
    // This method is also called but through a different instance with first one.
    self = [super initWithFrame:frame];
    NSLog(@"MyOutlineView initWithFrame(valid)::%@", self);
    return self;
}

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
    NSLog(@"MyOutlineView data source delegate(valid)::%@", self);
    return 0;
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
    return nil;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
    return NO;
}

@end

//
//  MyTextView.h

#import <Cocoa/Cocoa.h>

@interface MyTextView : NSTextView

@end

//
//  MyTextView.m

#import "MyTextView.h"

@implementation MyTextView

- (id)initWithCoder:(NSCoder *)aDecoder
{
    // This method is called.
    self = [super initWithCoder:aDecoder];
    NSLog(@"MyTextView initWithCoder::%@", self);
    return self;
}

- (id)initWithFrame:(NSRect)frame
{
    // But this method is NOT called at all.
    self = [super initWithFrame:frame];
    NSLog(@"MyTextView initWithFrame::%@", self);
    return self;
}

@end

输出:

MyTextView initWithCoder::                 [MyTextView: 0x10013be80]
MyOutlineView initWithCoder(INVALID)::     [MyOutlineView: 0x10014bc90]
MyOutlineView initWithFrame(valid)::       [MyOutlineView: 0x1001604a0]
MyOutlineView data source delegate(valid)::[MyOutlineView: 0x1001604a0]
AppDelegate.myOutlineView(INVALID)::       [MyOutlineView: 0x10014bc90]
AppDelegate.myTextView::                   [MyTextView: 0x10013be80]

正因为如此,我不得不把“AppDelegate.myOutlineView = self;”进入 MyOutletView 的实现,无论它调用 AppDelegate 的相关方法。这似乎不自然。

【问题讨论】:

  • 我看不到您在代码中记录AppDelegate.myOutlineView(INVALID):: 的位置。
  • 你为什么使用 initWithFrame:?您在 IB 中创建的视图没有必要。只需删除它,您的问题就会消失。
  • 我不需要那个。无论我是否使用 initWithFrame: ,应用程序都会实例化这两个必须彼此相同的不同实例。
  • 顺便说一下,自定义视图或 NSTextView 等其他 NSView 派生类对此没有任何问题。它们的工作方式与我们的想法完全一样。我认为这个问题只针对 NSOutlineViews。

标签: xcode cocoa


【解决方案1】:

Xcode 似乎不允许您将大纲视图的委托或数据源设置为自身。

所以我猜你正在做这样的事情:

也就是说:实例化您的自定义大纲视图类的第二个副本。

这是此设置的输出:

2012-09-26 14:11:34.511 testproj[30255:403] -[MyOutlineView initWithCoder:]
2012-09-26 14:11:34.531 testproj[30255:403] -[MyOutlineView initWithFrame:]

通过删除“我的大纲视图”的额外(突出显示)实例,initWithFrame: 行将消失。

要使大纲视图成为自己的委托,请改为:

- (void) awakeFromNib {
    self.delegate = self;
}

也就是说,委托模式的重点是避免子类化的需要。如果您确实需要大纲视图子类,请尝试直接覆盖 NSOutlineView / NSTableView 方法,而不是使用委托协议。

【讨论】:

  • 你指点一下!谢谢一丢丢!我喜欢亲吻你和所有人。我向你学习了一些。祝你早晚愉快!
【解决方案2】:

我无法重现您的问题。我删除了您发布到测试应用程序中的所有代码,并且我只获得了每个对象的一个​​实例化。当我尝试时,两个 initWithFrame 方法都没有被调用。我的输出是:

2012-09-26 09:00:38.945 TextViewDoubleInstantiationProblem[451:303] MyTextView initWithCoder::<MyTextView: 0x100123990>
    Frame = {{0.00, 0.00}, {381.00, 182.00}}, Bounds = {{0.00, 0.00}, {381.00, 182.00}}
    Horizontally resizable: NO, Vertically resizable: YES
    MinSize = {381.00, 182.00}, MaxSize = {463.00, 10000000.00}
2012-09-26 09:00:38.953 TextViewDoubleInstantiationProblem[451:303] MyOutlineView initWithCoder(INVALID)::<MyOutlineView: 0x101a1cb90>
2012-09-26 09:00:39.005 TextViewDoubleInstantiationProblem[451:303] AppDelegate.myOutlineView(INVALID)::<MyOutlineView: 0x101a1cb90>
2012-09-26 09:00:39.005 TextViewDoubleInstantiationProblem[451:303] AppDelegate.myTextView::<MyTextView: 0x100123990>
    Frame = {{0.00, 0.00}, {381.00, 182.00}}, Bounds = {{0.00, 0.00}, {381.00, 182.00}}
    Horizontally resizable: NO, Vertically resizable: YES
    MinSize = {381.00, 182.00}, MaxSize = {463.00, 10000000.00}

您的应用中是否还有其他未显示的代码?

【讨论】:

  • 你错过了一件事。你的输出和我的不一样。声明 委托并实现 outlineView:numberOfChildrenOfItem: 方法并放入 "NSLog(@"%@", self);"进入那里,然后你会看到不同的实例。 AppDelegate.myOutlineView 等于 MyOutlineView initWithCoder: 中的“自我”。我首先发布的代码是完整的。
  • 我确实有该代码,但该方法从未被调用。你是如何设置大纲视图的委托的。我在您的代码中没有看到它,并且您无法在 IB 中将大纲视图的委托设置为自身。
  • 这个答案似乎是正确的。 @Wisebee,听起来您正在某处的笔尖中实例化自定义子类的第二个副本。您是否有一个蓝色的 NSObject 占位符,该类设置为您的 MyOutlineView 类?
  • 我可以通过这样做复制您的问题。
  • 哇,完全是我的错。我只是意识到如何使用 NSObject 占位符的方式。谢谢大家。
【解决方案3】:

对initWithCoder: 的调用来自加载在 nib 文件中定义的对象。自从您提到创建网点以来,我认为这就是您想要发生的事情。在这种情况下,他们打电话给initWithFrame: 让我觉得比编码员更有可能“无效”。

我会在initWithFrame: 中设置一个断点并跟踪该调用的来源,以识别额外的分配。

【讨论】:

  • 完全正确。我已经完成了断点的事情。但我找不到可识别的线程。它只显示汇编代码,但 main()。
  • 如果你试图强迫它在initWithFrame:里面用NSLog(@"Called from: %@", [NSThread callStackSymbols]);解释怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 2014-01-01
相关资源
最近更新 更多