NSUndoManager - 撤消按钮未显示

时间:2011-11-13 22:28:51

标签: cocoa nsundomanager

我有一个会议的文件。 初始化会议时,我将undoManager设置为指向Document的undoManager。 同样,我的会议有与会者(人员名单)。每个Person对象只是指向我的会议的undoManager,而undoManager只是指向Document的指针。

我添加和删除与会者的撤消工作一直有效,直到我开始观察人员属性的关键值。

关于我做错的任何想法?添加和删​​除与会者时,撤消按钮不会激活。同样,当我更改此人的姓名/费率时,撤消按钮不会显示。

Document.m

- (id)init
{
    self = [super init];
    if (self) {
        self.meeting = [[Meeting alloc] init];
        self.meeting.undoManager = self.undoManager;

meeting.h ---

@property (nonatomic, retain) NSUndoManager *undoManager;

meeting.m

- (void)changeKeyPath:(NSString *)keyPath
         ofObject:(id)obj
          toValue:(id)newValue {
// setValue:forKeyPath: will cause the key-value observing method
// to be called, which takes care of the undo stuff
[obj setValue:newValue forKeyPath:keyPath];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context {
    id oldValue = [change objectForKey:NSKeyValueChangeOldKey]; 
    // NSNull objects are used to represent nil in a dictionary
    if (oldValue == [NSNull null]) {
        oldValue = nil;
}

    [[self.undoManager prepareWithInvocationTarget:self] changeKeyPath:keyPath
                                                          ofObject:object
                                                           toValue:oldValue];
   // Notify the undoManager
   self.undoManager.actionName = @"Edit";

}

- (void)startObservingPerson:(Person *)person {
    // TODO: Understand if I need something for context
    [person addObserver:self
         forKeyPath:@"name"
            options:NSKeyValueObservingOptionOld
            context:nil];

    [person addObserver:self
         forKeyPath:@"rate"
            options:NSKeyValueObservingOptionOld
            context:nil];

}

- (void)stopObservingPerson:(Person *)person    {
    [person removeObserver:self forKeyPath:@"name"];
    [person removeObserver:self forKeyPath:@"rate"];
}

-(void) insertObject:(id *)object inAttendeeListAtIndex:(NSUInteger)index {

    [(Person *)object setMeeting:self];
    // Enable undo capabilities for edits to the name/rate
    [self startObservingPerson:(Person *)object];
    // insert the object / person
    [self.attendeeList insertObject:(Person *)object atIndex:index];

    //
    // configure the undo for the insert
    [[self.undoManager prepareWithInvocationTarget:self] removeObjectFromAttendeeListAtIndex:(NSUInteger) index];

    undoManager.actionName = @"Insert Person";

}

-(void) removeObjectFromAttendeeListAtIndex:(NSUInteger)index {
    Person *deletedPerson = [self.attendeeList objectAtIndex:index];
    // housecleaning before removing the person
    [self stopObservingPerson:(Person *)deletedPerson];

    // remove the object / person
    [self.attendeeList removeObjectAtIndex:index];

    // configure the undo
    [[self.undoManager prepareWithInvocationTarget:self] insertObject:(id *)deletedPerson inAttendeeListAtIndex:index];

    // Notify the undoManager
    undoManager.actionName = @"Remove Person";

}

1 个答案:

答案 0 :(得分:0)

我找到了问题的答案。我以两种方式之一初始化会议。新文件和存档文件。当我从存档加载时,我没有分配undoManager所以它是null并且没有任何事情发生