简体   繁体   English

NSArrayController的rangedObjects看到自己的类有误

[英]NSArrayController’s arrangedObjects sees itself with wrong class

I'm trying to create a view-based NSTableView, populated via Cocoa bindings. 我正在尝试创建一个基于视图的NSTableView,并通过Cocoa绑定进行填充。

My NSArrayController is pointing to the jobs array property of the currentUser property of the application delegate, like so: 我的NSArrayController指向应用程序委托的currentUser属性的jobs数组属性,如下所示:

绑定1

I expect each model in that array to be of class HSJob : 我希望该数组中的每个模型都属于HSJob类:

绑定2

However, once I'm binding my table view to the NSArrayController, its arrangedObjects property for some reason thinks it belongs to class HSJob (I'm expecting an NSArray ). 但是,一旦我结合我的表视图NSArrayController的,其arrangedObjects财产因某种原因认为它属于类HSJob (我期待一个NSArray )。

绑定3

Obviously, this doesn't let me link individual cell content to Job object properties and I can't display anything in my table view: 显然,这不允许我将单个单元格内容链接到Job对象属性,并且不能在表格视图中显示任何内容:

绑定4

So what am I doing wrong here? 那我在做什么错呢?

Update 更新

I even decided to go as far as implementing some manual value update notifications + moving everything into the AppDelegate, just to experiment. 我什至决定尝试实施一些手动值更新通知+将所有内容移至AppDelegate中,以进行实验。 Here, I changed my own HSJob class for just some NSDictionaries. 在这里,我只为一些NSDictionaries更改了自己的HSJob类。

AppDelegate.h

@property NSArray *things;

AppDelegate.m

@synthesize things = _things;

- (NSArray *)things {
    if (!_things)
        return @[@{@"name": @"Default Thing"}];
    else
        return _things;
}

- (void)setThings:(NSArray *)things {
    [self willChangeValueForKey:@"things"];
    _things = things;
    [self didChangeValueForKey:@"things"];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    [self setThings:@[@{@"name": @"Actual Thing"}]];

}

Only “Default Thing” ever gets displayed. 仅显示“默认情况”。 Even though I'm setting a new value to the things property right after the application launch, that array never gets displayed. 即使我在应用程序启动后立即为things属性设置了新值,该数组也永远不会显示。

What you "did wrong" was to create an app delegate object in a XIB other than MainMenu.xib, and then assume that it's [NSApp delegate]. 您“做错了”是在除MainMenu.xib之外的XIB中创建一个应用程序委托对象,然后假定它是[NSApp委托]。 What you actually get is a totally separate object of your app delegate class with its own properties, just for that xib. 实际上,您得到的是应用委托类的一个完全独立的对象,具有其自己的属性,仅用于该xib。 So the object attached to your XIB never runs applicationDidFinish launching because it is not your app's delegate. 因此,附加到您的XIB的对象永远不会运行applicationDidFinish启动,因为它不是您应用程序的委托。

I suggest a solution by modifying the init for your app delegate class here: https://stackoverflow.com/a/22873562/3120884 我建议通过在此处修改应用程序委托类的init来提出解决方案: https : //stackoverflow.com/a/22873562/3120884

MainMenu.xib gets away with having an App Delegate object because as the NSMainNib designated in Info.plist, it gets special treatment in NSApplicationMain, which does hook up that Interface Builder object to [NSApp delegate]. MainMenu.xib放弃了具有App Delegate对象的功能,因为作为Info.plist中指定的NSMainNib,它在NSApplicationMain中得到了特殊处理,该应用程序确实将该Interface Builder对象连接到了[NSApp委托]。

I managed to fix this by binding my NSArrayController to Application instead of the manually created AppDelegate object. 我设法通过将NSArrayController绑定到Application而不是手动创建的AppDelegate对象来解决此问题。 Therefore, the Model Key Path is now: 因此,模型关键路径现在为:

self.delegate.currentUser.jobs

Obviously this doesn't get autocompleted by Xcode and it still complains about not being able to resolve the path, but it works anyway. 显然,Xcode不会自动完成此操作,并且它仍然抱怨无法解析路径,但是仍然可以正常工作。

The reason why my AppDelegate object in the Xib wasn't working is still a mystery, but it may have something to do with the fact that the Xib in question wasn't the default MainMenu.xib, but a custom NSPanel attached to a controller. 我的AppDelegate中的AppDelegate对象无法正常工作的原因仍然是个谜,但这可能与以下问题有关:所讨论的Xib不是默认的MainMenu.xib,而是连接到控制器的自定义NSPanel 。

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

相关问题 NSTreeController的arrangedObjects不响应子元素 - `NSTreeController`'s `arrangedObjects` doesn't respond to `children` NSArrayController用于多态类 - NSArrayController for polymorphic class NSArrayController在多个NIB中访问的Singleton NSMutableArray - Singleton NSMutableArray accessed by NSArrayController in multiple NIB's 如何将NSMutableArray绑定到NSArrayController的选择? - How to bind a NSMutableArray to NSArrayController's selection? 苹果的Sketch源代码NSArrayController绑定 - Apple's Sketch Source code NSArrayController Binding 如何使用与自定义类绑定到NSUserDefaultsController的NSArrayController - How to use NSArrayController bound to NSUserDefaultsController with custom class 与NSPopupButton不同,NSArrayController的selectedObjects链接到NSTableView选择 - Unlike to NSPopupButton, NSArrayController's selectedObjects is linked to NSTableView selection 从controlTextDidEndEditing委托方法访问NSArrayController的绑定数组 - Accessing NSArrayController's bound array from controlTextDidEndEditing delegate method 如果在“自动重新排列内容”中,NSArrayController上的setFilterPredicate不起作用 - setFilterPredicate on a NSArrayController doesn't work if it's in “Auto Rearrange Content” 目标C获取选定行的键的NSArrayController的值 - Objective C get NSArrayController's values for keys of selected row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM