cocoa - 首选项 Pane 和 NSTableView 绑定(bind)

标签 cocoa cocoa-bindings

我正在尝试创建一个位于系统首选项中的首选项 Pane 。所有绑定(bind)都像往常一样完成(对于普通窗口应用程序),但是当调用绑定(bind)属性的 setter (数据更新)时,表数据不会重置。首选项 Pane 是否能够通过绑定(bind)更新表数据?我也尝试过使用表数据源,但没有成功。

为了澄清,我的 prefPane 的主类中有一个 NSMutableArray 属性,一个代表 prefPane 主类的对象,以及 IB 中绑定(bind)到表列的 arrayController 。在 prefPane 主类的 init 方法中,我设置了 NSMutableArray 的值,该值正确反射(reflect)在 pref Pane 中,但是,(只是为了测试绑定(bind)是否有效),我有一个 NSTimer,它重置 NSMutable 数组的值当它完成时。控制台消息告诉我该值已正确重置,但是更改并未反射(reflect)在首选项 Pane 中。

因此,在我当前的版本中,我使用以下代码将属性设置为任意值(简化为尝试使绑定(bind)​​完全正常工作)。 10 秒后,计时器会重置该属性值。尽管属性已正确更新(通过控制台日志验证),但首选项 Pane 并未反射(reflect) TableView 中的更改。不幸的是,我无法发布绑定(bind)的屏幕截图。我在 IB 中有一个用于syncFrontEndPref 类的对象。然后,我将一个数组 Controller 绑定(bind)到该对象,并带有 listArray 的模型键路径。然后我的表列绑定(bind)到数组 Controller 排列的对象。这会在首选项 Pane 中正确加载“test”、“test1”、“test2”(从 init 方法填充)。但是,当从计时器重新填充时,更改不会反射(reflect)在首选项 Pane 中(尽管控制台日志确认 listArray 确实已更改。

代码如下:

@interface syncFrontEndPref : NSPreferencePane 
{

    NSMutableArray *listArray;
    NSNumber *syncInt;
    AuthenticateUser *newUser;
    NSMutableArray *syncIntervalList;
    IBOutlet NSTableView *theTableView;

}

@property (retain) NSMutableArray *listArray;
@property (retain) NSMutableArray *syncIntervalList;

- (void) mainViewDidLoad;
-(IBAction)syncIntervalValueChanged:(id)sender;
-(IBAction)tableViewSelected:(id)sender;



@implementation syncFrontEndPref

@synthesize listArray, syncIntervalList;

-(id) init{

    //populate nsarray w/ list data to display
    //[self setListArray: [NSMutableArray arrayWithArray:[[[NSDictionary dictionaryWithContentsOfFile:[GetFilePath pathForFile]] objectForKey:@"lists"] allObjects]]];
    [self setListArray: [NSMutableArray arrayWithObjects: @"test", @"test1", @"test2", nil]];

    //define values for drop-down sync interval selector
    [self setSyncIntervalList:[NSMutableArray arrayWithObjects: @"1 minute", @"5 minutes", @"10 minutes", @"30 minutes", @"24 hours", nil]];

    return self;

}

//code for the timer and selector method
- (void) mainViewDidLoad{
NSTimer *timer = [[NSTimer new] autorelease];
    int syncTime = 10;
    timer = [NSTimer scheduledTimerWithTimeInterval: syncTime target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES];
}

-(void)targetMethod:(id)sender{

NSLog(@"running timer...");

[self setListArray: [NSMutableArray arrayWithObjects: @"0", @"1", @"2", nil]];
 NSLog(@"%@", listArray);   
}

最佳答案

我认为您实例化了两个syncFrontEndPref 对象实例。

如果您从模板创建首选项 Pane 项目,文件的所有者将是 NSPreferencePane。如果您为syncFrontEndPref 对象添加了另一个条目,您将创建该对象的第二个副本,并且在第二个副本中不会调用mainViewDidLoad。该对象副本不会触发计时器,并且 listArray 也不会更新。尝试在 init 方法中添加一条日志语句。如果您看到该日志语句运行两次,则您有该对象的两个副本。

如果您确实有该对象的两个副本,我建议删除您添加到 IB 中的 xib 的副本。将文件所有者的类更改为您的syncFrontEndPref 类,并将您的绑定(bind)连接到该对象。

<小时/>

这看起来像 IB 中当前的 xib 文件吗?

alt text

关于cocoa - 首选项 Pane 和 NSTableView 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4161044/

相关文章:

objective-c - 绑定(bind)返回默认值(使用 registerDefaults :) instead of zero 设置

cocoa - 持续将日志文件的内容写入 NSTextview

cocoa - NSScrollView 是如何工作的?

python - 如何使用 Python 脚本中的 Cocoa API 在 macOS 中最小化/最大化窗口?

ios - Objective C NSString* 属性保留计数奇数

iphone - 如何实现任务完成

cocoa - KVO - 从存储中检索对象并显示版本时出现问题

cocoa - NSTableView 和 NSCollectionView 之间的区别

data-binding - 究竟什么是绑定(bind)?

objective-c - Mac 10.6 及更高版本的可用启动磁盘空间错误