Corbin's Treehouse - Corbin Dunn, Santa Cruz, CA
Plug Bug

The 'Cocoa' Category

Cocoa: Targeting the entire tableview in a drop operation

Hi Programmers. If you are doing drag and drop in an NSTableView with Cocoa, you can use the following method in your “validateDrop” datasource method to retarget the row it is going to: – (void)setDropRow:(int)row dropOperation:(NSTableViewDropOperation)op; The documentation is missing something. If you specify a row of -1 and a dropOperation of NSTableViewDropOn, it will target the entire tableview, as opposed to an individual row.

Cocoa: drop down menu buttons

In Cocoa, it is pretty easy to use an NSButton that creates a drop down menu. One of the key-tricks to making it appear to be an NSPopUpButton is to set the [NSCell sendActionOn:]. Generally, menus are displayed on mouse down, not up. Normally, the action for a button is sent on mouse up. To get around this, you can simply access the cell of your button and tell it to send the action on mouse down: [[mybutton cell] sendActionOn:NSLeftMouseDownMask]; Cool!

Cocoa: Creating a custom Color Picker in Cocoa. Part 1.

It is pretty easy to create a custom Color Picker that is available in any application, or for just your particular app. It isn’t difficult to do, but there are some caveats that you must be aware of. I’m going to go over this as a step-by-step tutorial; we need more Cocoa tutorials out there, so this will be one of them! Start by creating a new Cocoa Bundle inside of Xcode (File | New Project…) Now, be careful what you name your bundle — the name must correspond to the main color picker’s classname. Once you have your project... [read more]

Cocoa: Using NSPredicate and NSMetadataQuery

Hi Apple Cocoa developers. Here are some more tips and tricks for Cocoa development. On Tiger, there is a new class called NSMetadataQuery that allows you to do some cool Spotlight searches. See the “Spotlighter” demo application included in /Developer/Examples/AppKit/Spotlighter. Creating a predicate to get the search results that you want can be tricky, but alas, there is an easy way! First, do a search inside of Finder that has the criteria you want. For example, below is a search for all images recently created: Hit the save button and save it to your desktop. You can then show the... [read more]

Cocoa: Disappearing headers in CoreData generated TableViews

Apple Cocoa developers: Finally another blog entry for you! Everyone else: You may want to skip this one… It has come to my attention that some NSTableView instances created by dragging CoreData objects into Interface Builder will not work quite right. Specifically, if you add one more row from a case like this: you will sometimes see the header disappear: Looking at the NSTableView in IB reveals that it has an offset frame location, which you cannot change: The solution is to add an outlet to the NSTableView inside your controller and add a bit of code to correct the... [read more]

Removing sort descriptors on a sorted NSTableView / NSOutlineView

I’ve discovered that some people may want to programatically remove the sort descriptors for their tableview: This is easy to do with a little code: - (IBAction)removeSorting:(id)sender { [tableView setSortDescriptors:[NSArray array]]; } However, you may want your users to be able to do it. It is fairly simple to add a button in the corner view of your table that does this: In your awakeFromNib method you can easily add a bit of code that adds this button and invokes the above “removeSorting:” method: NSRect buttonFrame = [[tableView cornerView] frame]; NSButton *button = [[NSButton alloc] initWithFrame:buttonFrame]; [button setBezelStyle:NSSmallSquareBezelStyle]; [button setTitle:@“X”];... [read more]

Cocoa only feed

One of the great things about WordPress is that you can subscribe to the feed that you are intersted in. For instance, if you just interested in Cocoa stuff, you can just watch this feed. To subscribe to a different feed, change the cat=6 to whatever category number you want. Very cool. I like WordPress. Eventually I may add subscribe links to all my pages. In fact..i might do that now.

Yet another way to make connections in Interface Builder (IB)

Some of IB’s features aren’t readily apparent. Most people know about the mode to switch from icon to text by clicking the icon in the lower right hand section seen in this image: But, when you are in that mode, did you know you can make connections with the normal ctrl-drag? Cool, huh! This can give you fine-grained control when it is sometimes hard to select certain items.

NSOutlineView, reloading items, and the expansion state

NSOutlineView requires all of the items in it to be pointer unique. If they are not, strange things happen. However, they can be equal (meaning [NSObject isEqual] may return YES). However, there is a small exception to that rule. If you do a reload, the expansion state of items will be preserved. This expansion state is done by placing the current expanded items into an NSMutableSet. After a reload happens, if an item is in that set it is shown expanded. What does this mean? Well, to see if an item should be expanded or not, it is looked up... [read more]

Cmd – click. The subtle secret of Mac OS X.

It seems that a lot of people don’t know how cmd click works in a lot of Mac OS X Cocoa applications. To put it simply, Cmd-clicking on a non key window (or application for that matter) will act like a normal click without making the window key! This is really cool for testing UI things while debugging or testing for memory leaks. Here is how I use this technique while debugging: 1. I’m debugging my application with Xcode. Inside of Xcode I have a “hot” breakpoint that I don’t want to enable, since enabling it will make Xcode key,... [read more]

Drag and Drop in an NSTableView

Drag and Drop in an NSTableView is easy to do. However, I think the documentation (Table Views: Using Drag and Drop in Tables) for it isn’t particularly great. It misses a few points, so I’m going to go over the basic steps on how to add drag and drop to your TableView. Here, I’ll assume you have a TableView with your source code controller class set as the delegate. Declare your custom pasteboard format: #define BasicTableViewDragAndDropDataType @“BasicTableViewDragAndDropDataType” In awakeFromNib you must register for the drag types you want to receive (you could have others here): – (void)awakeFromNib {    ... [read more]

Dynamically populating an NSPopUpButtonCell in an NSTableView

It is quite common. You have a PopUpButton (NSPopUpButtonCell) in an NSTableView and you want to dynamically change the contents based on the selected row: How do you do this? There are a few tricky steps. First, add a menu to the nib and set the delegate for the menu to be your Controller: Okay. Next is the tricky part. In IB, when you have a column in a tableview selected, it has a little white triangle in the corner: Clicking on that will allow you to modify properties of the cell (in this case, the NSPopUpButtonCell). However, we want... [read more]

Different cells on each row in an NSTableView or NSOutlineView

Some people have asked me how to dynamically change the cell that is displayed for each row in an NSTableView or NSOutlineView. Generally, the same cell is used for each row, but it is possible to use a different cell for each row, if you like. NSTableColumn can change the cell that is used for each row. NSTableView calls -[NSTableColumn dataCellForRow:(int)row] for each row. Now, consider this code: @interface NSObject (VariableCellColumnDelegate) - (id)tableColumn:(NSTableColumn *)column inTableView:(NSTableView *)tableView dataCellForRow:(int)row; @end @interface VariableCellColumn : NSTableColumn @end @implementation VariableCellColumn - (id)dataCellForRow:(int)row { id delegate = [[self tableView] delegate]; if ([delegate respondsToSelector:@selector(tableColumn:inTableView:dataCellForRow:)]) { return [delegate... [read more]

Debugging OCTest bundles

Another old article: June 20, 2005 Debugging OCTest bundles To debug OCTest bundles: 1. Add a new executable to your Xcode project pointing it to “otest” at /Developer/Tools/otest 2. Double click on the executable, and add two run params (requires Xcode 2.1):   a. -SenTest Self   b. $(BUILT_PRODUCTS_DIR)/YourBundleName.octest This makes it REALLY easy to debug OCUnit tests. You can debug an individual test suite with:   -SenTest UnitTestClassName or an individual test case with:   -SenTest UnitTestClassName/testMethodName For instance, here is what one of my arguments looks like in Xcode 2.1: I can easily test/debug all tests, or an individual... [read more]

Repost: How to find memory leaks in Cocoa apps with Object Alloc

(The original of this I accidentally killed — here is a copy). If your Cocoa application leaks memory, here is a way to find those leaks! 1) Open your application in Object Alloc 2) Start the process, and check to have retain events: 3) Run to a known state 4) Check "Show since mark" 5) Click "Auto sort" 6) Click the "Current" column header to sort on that automatically 7) Click the Mark button 8) Do the offending memory leak operation (to warm it up) 9) Click Mark again, and repeat the offending memory leak operation 10) Take a look... [read more]

Changing the disclosure triangle in an NSOutlineView

At WWDC I was asked how to remove the disclosure triangle in an NSOutlineView. Well, first things first. You can change it with this bit of code in your delegate: - (void)outlineView:(NSOutlineView *)ov willDisplayOutlineCell:(NSButtonCell *)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item { [cell setImage:[NSImage imageNamed:@“collapsedglyph.tiff”]]; [cell setAlternateImage:[NSImage imageNamed:@“expandedglyph.tiff”]]; } If you want to set it to nil, you will have to create an image that is empty. –corbin

Tooltips for NSTableView cell’s in Tiger

At WWDC, I quickly mentioned how easy it is to add tooltip’s to an NSCell for an NSTableView/NSOutlineView. Here is a quick snippet of code on how to do this only if the text doesn’t fill up the entire cell: - (NSString *)tableView:(NSTableView *)tv toolTipForCell:(NSCell *)cell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)tc row:(int)row mouseLocation:(NSPoint)mouseLocation { if ([cell isKindOfClass:[NSTextFieldCell class]]) { if ([[cell attributedStringValue] size].width > rect->size.width) { return [cell stringValue]; } } return nil; } You will obviously have to set the delegate for the tableview to be whatever class implements the above method, and this will only work on Tiger. But, it... [read more]

Drawing a “mail like” border on items in an NSTableView

Mail has a cool way of making unread messages stand out. It is really easy to do this type of thing with NSTableView/NSOutlineView. Subclass the one you want, and override drawRow. Toss in the code you see below, and it should give a cool highlight on all expandable rows in an NSOutlineView (you will have to modify it to have it work in NSTableView — just remove the call to isExpandable). - (void)drawRow:(int)row clipRect:(NSRect)clipRect { if (([self isExpandable:[self itemAtRow:row]]) && (![self isRowSelected:row])) { // Draw a light-blue “mail like” border around the row, if not selected NSRect rect = [self... [read more]

(c) 2008-2017 Corbin Dunn

Corbin's Treehouse is powered by WordPress. Made on a Mac.

Subscribe to RSS feeds for entries and comments.

51 queries. 0.530 seconds.