设为首页 收藏本站
查看: 854|回复: 0

[经验分享] NSUndoManager(Chapter 9 of Cocoa Programming for Mac OS X)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-12-30 13:41:34 | 显示全部楼层 |阅读模式
DSC0000.gif DSC0001.gif Document

  1 //
  2 //  MyDocument.m
  3 //  RaiseMan
  4 //
  5 //  Created by b1mobile on 2/14/11.
  6 //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7 //
  8
  9 #import "MyDocument.h"
10 #import "Person.h"
11
12 @implementation MyDocument
13
14 - (id)init
15 {
16     self = [super init];
17     employees = [[NSMutableArray alloc] init];
18     return self;
19 }
20
21 - (void)dealloc
22 {
23     [super setEmployees:nil];
24     [super dealloc];
25 }
26
27 - (void)setEmployees:(NSMutableArray *)a
28 {
29     if(a == employees)
30     {
31         return;
32     }
33     
34     for(Person *person in employees)
35     {
36         [self stopObservingPerson:person];
37     }
38     
39     [a retain];
40     [employees release];
41     employees = a;
42     
43     for(Person *person in employees)
44     {
45         [self startObservingPerson:person];
46     }
47 }
48
49 - (void)insertObject:(Person *)p inEmployeesAtIndex:(int)index
50 {
51     NSLog(@"adding %@ to %@", p, employees);
52     NSUndoManager *undo = [self undoManager];
53     [[undo prepareWithInvocationTarget:self] removeObjectFromEmployeesAtIndex:index];
54     if(![undo isUndoing])
55     {
56         [undo setActionName:@"Insert Person"];
57     }
58     [self startObservingPerson:p];
59     [employees insertObject:p atIndex:index];
60 }
61
62 - (void)removeObjectFromEmployeesAtIndex:(int)index
63 {
64     Person *p = [employees objectAtIndex:index];
65     NSLog(@"removing %@ from %@", p, employees);
66     NSUndoManager *undo = [self undoManager];
67     [[undo prepareWithInvocationTarget:self] insertObject:p inEmployeesAtIndex:index];
68     
69     if(![undo isUndoing])
70     {
71         [undo setActionName:@"Delete Person"];
72     }
73     [self stopObservingPerson:p];
74     [employees removeObjectAtIndex:index];
75 }
76
77 - (void)startObservingPerson:(Person *)person
78 {
79     [person addObserver:self forKeyPath:@"personName" options:NSKeyValueObservingOptionOld context:NULL];
80     [person    addObserver:self forKeyPath:@"expectedRaise" options:NSKeyValueObservingOptionOld context:NULL];
81 }
82
83 - (void)stopObservingPerson:(Person *)person
84 {
85     [person removeObserver:self forKeyPath:@"personName"];
86     [person    removeObserver:self forKeyPath:@"expectedRaise"];
87 }
88
89 - (void)changeKeyPath:(NSString *)keyPath ofObject:(id)obj toValue:(id)newValue
90 {
91     [obj setValue:newValue forKeyPath:keyPath];
92 }
93
94 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
95 {
96     NSUndoManager *undo = [self undoManager];
97     id oldValue = [change objectForKey:NSKeyValueChangeOldKey];
98     
99     if(oldValue == [NSNull null])
100     {
101         oldValue = nil;
102     }
103     
104     NSLog(@"oldValue = %@", oldValue);
105     [[undo prepareWithInvocationTarget:self] changeKeyPath:keyPath ofObject:object toValue:oldValue];
106     [undo setActionName:@"Edit"];
107 }
108
109 - (IBAction)createEmployee:(id)sender
110 {
111     NSWindow *w = [tableView window];
112     
113     BOOL editingEnded = [w makeFirstResponder:w];
114     if(!editingEnded)
115     {
116         NSLog(@"Unable to end editing");
117         return;
118     }
119     
120     NSUndoManager *undo = [self undoManager];
121     if ([undo groupingLevel])
122     {
123         [undo endUndoGrouping];
124         [undo beginUndoGrouping];
125     }
126     Person *p = [employeeController newObject];
127     [employeeController addObject:p];
128     [p release];
129     [employeeController rearrangeObjects];
130     
131     NSArray *a = [employeeController arrangedObjects];
132     int row = [a indexOfObjectIdenticalTo:p];
133     NSLog(@"starting edit of %@ in row %@", p, row);
134     [tableView editColumn:0 row:row withEvent:nil select:YES];
135 }
136
137 - (NSString *)windowNibName
138 {
139     // Override returning the nib file name of the document
140     // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
141     return @"MyDocument";
142 }
143
144 - (void)windowControllerDidLoadNib:(NSWindowController *) aController
145 {
146     [super windowControllerDidLoadNib:aController];
147     // Add any code here that needs to be executed once the windowController has loaded the document's window.
148 }
149
150 - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
151 {
152     // Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil.
153
154     // You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
155
156     // For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:. In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
157
158     if ( outError != NULL ) {
159         *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
160     }
161     return nil;
162 }
163
164 - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
165 {
166     // Insert code here to read your document from the given data of the specified type.  If the given outError != NULL, ensure that you set *outError when returning NO.
167
168     // You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
169     
170     // For applications targeted for Panther or earlier systems, you should use the deprecated API -loadDataRepresentation:ofType. In this case you can also choose to override -readFromFile:ofType: or -loadFileWrapperRepresentation:ofType: instead.
171     
172     if ( outError != NULL ) {
173         *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
174     }
175     return YES;
176 }
177
178 @end
179   

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-158403-1-1.html 上篇帖子: mac os x 10.9.1 安装 Homebrew软件包管理工具及brew安装maven3.1.1 下篇帖子: 转载Download iOSOpenDev for Mac OS X
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表