开发者

Cocoa: Stumped on interacting with UI objects using CoreData & Bindings

I'm a LAMP developer but new to Cocoa and building desktop apps in general. Been playing around with XCode and IB all week making a simple Task Manager to learn the frameworks and get comfortable with Objective-C. Done most of the tutorials on cocoadevcentral, been skimming over the Apple Docs and readi开发者_StackOverflowng Cocoa Programming For Mac OSX.

I've successfully built the DataModel (using Core Data), the UI with 2 NSTableViews (Projects and Tasks), add/remove buttons for both TableViews, and connected the bindings between NSArrayControllers and the TableViews. I can add projects, and tasks within each project. Works like a charm.

Data Model:

  • Project: projectName, tasks (relationship)
  • Task: taskName, estDuration, dueDate, status, project (relationship)

I'm just missing something. I've been stumbling with how to programmatically alter the UI elements before rendering and when a user clicks on rows or columns in the NSTableViews. I know how to create a class in XCode and bind it as a delegate in IB. Seems a bit limited in what I can respond to. I've subclassed the Project Entity in the Data Model to try and detect inserts & removes from the NSTableView, but this doesn't seem the right way to do it.

A subclass I was testing with:

// ProjectManagedObject.h
#import <Cocoa/Cocoa.h> 

@interface ProjectManagedObject : NSManagedObject {

}

@end


// ProjectManagedObject.m
#import "ProjectManagedObject.h"

@implementation ProjectManagedObject

- (void)awakeFromInsert
{
    [super awakeFromInsert];
    NSLog(@"new project inserted");
}

- (void)awakeFromFetch
{
    [super awakeFromFetch];
    NSLog(@"project awakeFromFetch");
}

- (BOOL)validateForDelete:(NSError **)error
{
    NSLog(@"project: validating for delete");       
    return YES;
}

@end

I'm also aware of Notifications and how they basically work. Just haven't tested them yet. But I'm wondering what I need to do next (delegates, notifications, subclass, ??) to have UI object access from a class I've created to do some of the following:

  • Before data gets displayed, format the duration for each task. Example: from an int "30" (Data Model) to a string "30m" (displayed in table column)
  • Click on a Task's duration to edit. Enter 2h, and hit enter. 2h then gets converted from 2h to 120 to be stored in Core Data source.
  • Adding a new Project adds the new Project(does this already) in it's NSTableView, but then places focus on the new row and makes it editable.

Any ideas about the direction I need to take now and/or ways to conqurer some of the above is greatly appreciated. Thanks.


Your three points are answered by:

  • the bindings documentation (especially the part that regards value transformers);
  • Formatters; and
  • You will have to produce a custom action that creates that object, sets it as the selection (see the Selection binding of your array controller and the bindings reference above), and then messages the NSTableView to enter editing mode.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜