我有一些简短的 iOS Objective-C 示例代码(从一个更大的项目中简化)导致 NSUndoManager 崩溃,我无法解释。
也就是说,当一个对象只被 NSUndoManager 释放时(因为它超出了撤消的级别),并且根据 docs调用 removeAllActionsWithTarget:self
,我得到一个 EXC_BAD_ACCESS。
// SimpleViewController.m
@interface ViewController ()
@property (nonatomic, strong) NSUndoManager *undoManager;
@end
@implementation ViewController
@synthesize undoManager;
// called from a simple button
- (IBAction)doItTappedid)sender
{
CoolObject *object = [CoolObject new];
object.undoManager = self.undoManager;
// according to docs, object will be retained by NSUndoManager here
// but target will not (which should be okay)
[self.undoManager registerUndoWithTarget:self selectorselector(notCool objectbject];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.undoManager = [NSUndoManager new];
self.undoManager.levelsOfUndo = 3;
}
和
// CoolObject.m
@implementation CoolObject
- (void)dealloc
{
[self.undoManager removeAllActionsWithTarget:self];
}
@end
在第四次点击按钮后 (levelsOfUndo
+ 1),它会崩溃。
如果我用 GCUndoManager 交换 NSUndoManager ,没有崩溃。
在 iOS 10.2 sim 和设备中测试。
感谢您的任何想法!
您可能会遇到此错误,因为 self.undoManager
在您使用它的时候没有保留。当对象已经被释放并且你尝试访问它时,你会得到错误的访问异常。
尝试从这里更改您的代码:
CoolObject *object = [CoolObject new];
到这里:
@interface ViewController (){
CoolObject *object;
}
@property (nonatomic, strong) NSUndoManager *undoManager;
@end
@implementation ViewController
- (IBAction)doItTappedid)sender
{
object = [CoolObject new];
object.undoManager = self.undoManager;
// according to docs, object will be retained by NSUndoManager here
// but target will not (which should be okay)
[self.undoManager registerUndoWithTarget:self selectorselector(notCool objectbject];
}
@end
希望这会有所帮助。
关于ios - NSUndoManager removeAllActionsWithTarget 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41602669/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) | Powered by Discuz! X3.4 |