NSUndoManager语言 - 撤销按钮不显示



我有一个文档,它有一个会议。当我初始化会议时,我将undoManager设置为指向文档的undoManager。同样,我的会议也有与会者(Person列表)。每个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";
}

我找到了问题的答案。我用两种方式来启动会议。新文件和通过存档的文件。当我从存档中加载时,我没有分配undoManager所以它是空的,什么也没发生I

相关内容

  • 没有找到相关文章

最新更新