-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIconStyleArrayController.m
81 lines (67 loc) · 2.42 KB
/
IconStyleArrayController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// IconStyleArrayController.m
// Add Folder Icons
//
// Created by Andrew Hodgkinson on 24/01/2011.
// Copyright 2011 Hipposoft. All rights reserved.
//
// This subclass exists only so that a default sort descriptor (by name
// ascending) can be easily applied to any place where an icon style list
// is generated, without duplicating code or convoluted bindings.
//
#import "IconStyleArrayController.h"
@implementation IconStyleArrayController
/* Overriding all three NSArrayController initialisation methods is probably
* overkill; it seems to me that there must be a more elegant and less
* overweight way to achieve this apparently simple requirement of specifying
* default sorting for a Core Data bound array controller. However, despite
* two days attempting to read around the topic on developer.apple.com and
* numerous articles turned up via Google, table views listing styles still
* always default to unsorted until a column heading is clicked upon, popup
* menus of styles are never sorted and so-on. In the end I abandoned various
* Interface Builder/defaults/bindings based schemes and just wrote the code
* below. Each NIB including one of these controllers will thus inherit the
* intended default sort order automatically.
*/
//TODO: There must be a better way! Revisit this sometime.
- ( instancetype ) init
{
self = [ super init ];
[ self setDefaultSortDescriptors ];
return self;
}
- ( instancetype ) initWithCoder: ( NSCoder * ) aDecoder
{
self = [ super initWithCoder: aDecoder ];
[ self setDefaultSortDescriptors ];
return self;
}
- ( instancetype ) initWithContent: ( id ) content
{
self = [ super initWithContent: content ];
[ self setDefaultSortDescriptors ];
return self;
}
- ( void ) setDefaultSortDescriptors
{
/* By name, ascending then by creation date, ascending, so that the sort
* order remains stable if a user defines styles with the same name.
*/
NSSortDescriptor * byNameDescriptor =
[
[ NSSortDescriptor alloc ] initWithKey: @"name"
ascending: YES
];
NSSortDescriptor * byDateDescriptor =
[
[ NSSortDescriptor alloc ] initWithKey: @"createdAt"
ascending: YES
];
NSArray * iconStyleSortDescriptors =
@[
byNameDescriptor,
byDateDescriptor
];
[ self setSortDescriptors: iconStyleSortDescriptors ];
}
@end