gpt4 book ai didi

swift - 简单的 NSPageController 示例抛出未知 subview 警告并停止工作

转载 作者:行者123 更新时间:2023-11-30 10:11:52 24 4
gpt4 key购买 nike

我正在尝试让一个非常基本的 NSPageController 工作(在书籍模式,而不是历史模式)。它将成功转换一次,然后停止工作。

我怀疑我创建的 NSImageViews 加载错误,但我不知道如何实现。

Storyboard有一个 SamplePageController,它保存在初始硬编码的 NSImageView 中。

我怀疑我在这里遗漏了一些非常明显的东西,因为我找到的所有关于 NSPageController 的教程都是 Objective C 的而不是 swift 的,并且倾向于关注历史 View 模式。

代码是:

import Cocoa

class SamplePageController: NSPageController, NSPageControllerDelegate {

private var images = [NSImage]()
@IBOutlet weak var Image: NSImageView!

//Gets an object from arranged objects
func pageController(pageController: NSPageController, identifierForObject object: AnyObject) -> String {
let image = object as! NSImage
let image_name = image.name()!
let temp = arrangedObjects.indexOf({$0.name == image_name})
return "\(temp!)"
}

func pageController(pageController: NSPageController, viewControllerForIdentifier identifier: String) -> NSViewController {
let controller = NSViewController()
let imageView = NSImageView(frame: Image.frame)
let intid = Int(identifier)
let intid_u = intid!
imageView.image = images[intid_u]
imageView.sizeToFit()
controller.view = imageView
return controller
// Does this eventually lose the frame since we're returning the new view and then not storing it and the original ImageView is long gone by then?
// Alternatively, are we not sizing the imageView appropriately?
}

override func viewDidLoad() {
super.viewDidLoad()
images.append(NSImage(named:"text")!)
images.append(NSImage(named:"text-2")!)
arrangedObjects = images
delegate = self
}
}

最佳答案

在这种情况下,您的 pageController.view 设置为您的 window.contentView 并触发警告。您需要做的是在 window.contentView 中添加一个 subview ,并让您的 pageController.view 指向它。

出现警告的原因是,由于 NSPageController 创建内容历史记录的快照( View ),因此它会将它们添加到与 pageController.view 相同的级别它们之间的转换:这意味着它将尝试将它们添加到 pageController.view.superview

如果您的 pageController.view 设置为 window.contentView,您将向 window.contentView.superview 添加 subview ,这是 not supported :

New since WWDC seed: NSWindow has never supported clients adding subviews to anything other than the contentView.

Some applications would add subviews to the contentView.superview (also known as the border view of the window). NSWindow will now log when it detects this scenario: "NSWindow warning: adding an unknown subview:".

Applications doing this will need to fix this problem, as it prevents new features on 10.10 from working properly. See titlebarAccessoryViewControllers for official API.

关于swift - 简单的 NSPageController 示例抛出未知 subview 警告并停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32545455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com