我有一些代码需要在应用程序终止时运行.我为NSApplicationWillTerminateNotification注册了我的控制器,如下所示:
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(applicationWillTerminate:)
name: NSApplicationWillTerminateNotification
object: nil];
Run Code Online (Sandbox Code Playgroud)
现在,如果我启动我的应用程序并在前20秒内退出它, applicationWillTerminate就会被调用.如果我稍后退出该应用程序,则不会.我的应用程序中可能会导致此行为?我还试图将我的控制器设置为NSApplication的委托,但结果相同.有任何想法吗?
谢谢.
哦,这是XCode 3.2,Snow Leopard 10.6.1,使用10.5 SDK.在Debug和Release版本中都会发生.
你可以这样打电话NSPasteboard:
[pboard declareTypes:types owner:self];
Run Code Online (Sandbox Code Playgroud)
这意味着粘贴板稍后会要求所有者根据需要提供类型的数据.然而,我从文档中找不到的东西(也许我错过了明显的流血事件),是否owner保留.
实际上令我担心的是,如果所有者是弱引用,它可能会被释放,如果粘贴板然后尝试从中请求数据,则会导致崩溃.
注意:我应该澄清一点,我对此更感兴趣的是帮助追踪错误,而不是让我的应用程序依赖它.但我也希望澄清文件.
NSToolbarItemGroup 在此处记录.关于它,我找不到更多的东西!
我正在尝试使用它,看看它的外观.我的代码基本上是直接从文档中提取的.但是,我从未在工具栏中看到"组".代码运行,项目被添加到工具栏,但它只是不可见?!
有没有人成功使用它?使用最新的XCode,环境是10.8.3.
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
{
return [NSArray arrayWithObject:@"GroupItem"];
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{
return [NSArray arrayWithObject:@"GroupItem"];
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem *item1 = [[NSToolbarItem alloc] initWithItemIdentifier:@"Item1"];
NSToolbarItem *item2 = [[NSToolbarItem alloc] initWithItemIdentifier:@"Item2"];
[item1 setImage:[NSImage imageNamed:NSImageNameQuickLookTemplate]];
[item2 setImage:[NSImage imageNamed:NSImageNameQuickLookTemplate]];
[item1 setLabel:@"Prev"];
[item2 setLabel:@"Next"];
NSToolbarItemGroup *group = [[NSToolbarItemGroup alloc] initWithItemIdentifier:@"GroupItem"];
[group setSubitems:[NSArray arrayWithObjects:item1, item2, nil]];
[group setLabel:@"Navigate"];
return group;
}
Run Code Online (Sandbox Code Playgroud) 关于:CoreAnimation: warning, deleted thread with uncommitted CATransactionmessage的堆栈溢出还有很多其他条目,
并通过启用对其进行排序CA_DEBUG_TRANSACTIONS.在我看到的99%的其他帖子中,回溯显示了未提交动画的来源(例如,参见核心动画警告:"未提交的CATransaction",其中显示了被调用的函数MyApp).
在我的情况下,我已经将CA_DEBUG_TRANSACTIONS标志设置为1并且我得到了回溯,但不幸的是它并没有告诉我它来自哪里:
Deallocating
CoreAnimation: warning, deleted thread with uncommitted CATransaction; created by:
0 QuartzCore 0x00007fff95f8c76a _ZN2CA11Transaction4pushEv + 312
1 QuartzCore 0x00007fff95f8c60a _ZN2CA11Transaction15ensure_implicitEv + 276
2 QuartzCore 0x00007fff95f916f5 _ZN2CA5Layer13thread_flags_EPNS_11TransactionE + 37
3 QuartzCore 0x00007fff95f91642 _ZN2CA5Layer4markEPNS_11TransactionEjj + 64
4 QuartzCore 0x00007fff95f931df _ZN2CA5Layer25set_needs_display_in_rectERK6CGRect + 315
5 AppKit 0x00007fff97e7ebdd _NSBackingLayerSetNeedsDisplayInRect + 319
6 QuartzCore 0x00007fff95f93081 -[CALayer setNeedsDisplay] + 62
7 AppKit 0x00007fff97e7ea77 -[NSView(NSInternal) _setLayerNeedsDisplayInViewRect:] + 648
8 …Run Code Online (Sandbox Code Playgroud) 我习惯于applicationDidFinishLaunching在 iOS 中执行应用程序初始化,但在 appKit 中它在主视图的方法之后被调用draw!由于应用程序尚未初始化,因此现在绘制任何内容还为时过早。这是顺序:
viewDidLoad (主视图控制器)draw(_ dirtyRect:) (主控制器视图)applicationDidFinishLaunching (为时已晚!)该API参考说,有关以下applicationDidFinishLaunching
在应用程序的主运行循环启动之后但在处理任何事件之前调用此方法。
因此,应该在之后 发布抽奖事件applicationDidFinishLaunching。我不习惯这种行为——这是怎么回事?
我放置了一个自定义类的实例,该实例BigView是IB NSView内部的子类NSScrollView。my的内容大小BigView将在运行时计算。设置内容大小的最佳做法是什么?
重写intrinsicContentSize,因为大书呆子牧场指南中的建议,并没有似乎工作-框架仍保持其原始尺寸:
class BigView: NSView {
...
override var intrinsicContentSize: NSSize { // doesn't work?!
return NSSize(width: 10000, height: 10000)
}
...
}
Run Code Online (Sandbox Code Playgroud)
以编程方式(或在IB中)设置框架确实有效:
class BigView: NSView {
...
override func awakeFromNib() {
...
self.frame = NSRect(x: 0, y: 0, width: 10000, height: 10000)
}
...
}I
Run Code Online (Sandbox Code Playgroud)
或从控制器:
class ViewController: NSViewController {
@IBOutlet weak var bigView: BigView!
...
override func viewDidLoad() {
super.viewDidLoad()
bigView.frame = NSRect(x: 0, y: …Run Code Online (Sandbox Code Playgroud) 我试图NSGridView通过子类化它并draw像这样覆盖它的方法来设置它的背景颜色:
class GridViewGreen: NSGridView
{ override func draw(_ dirtyRect: NSRect)
{ super.draw(dirtyRect)
let color = NSColor.green
let bp = NSBezierPath(rect: dirtyRect)
color.set()
bp.stroke()
print("drawing GridViewGreen")
}
}
Run Code Online (Sandbox Code Playgroud)
但该draw方法从未被调用。
我的 NSOutlineView 有一些组行无法折叠,类似于 Mail.app 无法隐藏“Mailboxes”组。我希望如果委托的shouldCollapseItem方法返回,则显示/隐藏悬停按钮将被禁用false,但情况似乎并非如此。
我尝试使用以下委托手动禁用它:
func outlineView(_ outlineView: NSOutlineView, willDisplayOutlineCell cell: Any, for tableColumn: NSTableColumn?, item: Any) {
if outlineView.delegate?.outlineView?(outlineView, shouldCollapseItem: item) == false {
if let view = outlineView.delegate?.outlineView?(outlineView, viewFor: tableColumn, item: item) {
if let button = view.subviews.first(where: { $0.identifier == NSOutlineView.showHideButtonIdentifier }) {
button.isHidden = true
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为返回的视图只是NSTableCellView将要呈现的视图,并且在调用此委托时它没有超级视图(因此我无法查找同级视图)。
我可以访问委托中的“显示/隐藏”按钮(这是一个未记录的NSOutlineButtonCell实例)makeView(withIdentifier:owner:),但此时我不知道它是否代表可以隐藏的组之一。
必须有一种方法可以做到这一点,因为 Mail.app(和其他应用程序)能够有条件地禁用组的显示/隐藏按钮。
我正在努力为现有 macOS 代码库中的自定义 UI 元素添加可访问性。我们有一个自定义错误窗口,它会在发生错误时出现,我正在尝试让 VoiceOver 在发生错误时宣布错误。
目前,我正在尝试使用该NSAccessibility.post(element: Any, notification: NSAccessibility.Notification, userInfo: [NSAccessibility.NotificationUserInfoKey : Any]?)方法,但是在运行代码时,没有说出通知。
这是我目前在 NSViewController 中的内容:
NSAccessibility.post(element: self, notification: .announcementRequested, userInfo: [
.announcement: NSLocalizedString("CANT_CONNECT_ERROR", comment: "Error string for connection failure"),
.priority: NSAccessibilityPriorityLevel.high
])
Run Code Online (Sandbox Code Playgroud)
我的期望是,使用该.announcementRequested选项,VoiceOver 应该会自动选择并说出公告,但没有运气。我尝试使用不同的通知类型,尝试在.announcement参数中放入一个硬编码的字符串值,并尝试使用.rawValue和90作为.priority参数。
Apple 文档指定NSWorkspace icon(forFile:)返回 32x32 图标,但我不确定如何指定不同的大小。
这是我用来显示 XCode 图标的示例 SwiftUI 代码:
Image(
nsImage: NSWorkspace.shared.icon(forFile: "/Applications/XCode.app/")
).renderingMode(.original)
Run Code Online (Sandbox Code Playgroud) appkit ×10
macos ×6
cocoa ×5
swift ×4
objective-c ×2
nspasteboard ×1
nsscrollview ×1
nstoolbar ×1
nsview ×1
nsworkspace ×1
quartz-core ×1
voiceover ×1
xcode ×1