gpt4 book ai didi

macos - 如何在 NSScrollView OS X (10.10) 中嵌入 Storyboard View Controller 以创建复杂的检查器 View ?

转载 作者:行者123 更新时间:2023-12-03 17:21:03 25 4
gpt4 key购买 nike

我正在观看 WWDC 2014 视频“212: Storyboards and controllers on OS X”。在这段视频中,他们声称 Pages UI 确实/可以使用 OS X 上的 Storyboards 进行排列(见下文)。

Screen capture from 212: Storyboards and controllers on OS X, showing storyboard layout in XCode 6

但是,在 Pages UI 中,检查器 View 非常长,并且嵌入在 ScrollView 中(您可以在 Page.app 检查器 View 中验证我的两根手指滚动),而且一些检查器项目本身也包含在(某些类型自定义)披露 View 。似乎不可能在 ScrollView 中嵌入 Storyboard View Controller ,因为没有对应的“ ScrollView Controller ”类。是这样吗?

如何将 Storyboard View Controller 的 View 嵌入到 Storyboard的 ScrollView 中?

我尝试过在运行时直接嵌入,但是,这是非常黑客的并且不能可靠地工作(自动布局问题)。这条路线仍然可能可行,但我想在走得太远之前寻求建议。对于真实的 UI,可能会回退到 XIB。

- (void)viewDidLoad {

[super viewDidLoad];

// Swap the view controllers' view for a scroll view
NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:self.view.frame];
scrollView.documentView = self.view;
scrollView.drawsBackground = NO;
self.view = scrollView;
}

最佳答案

我认为这个答案并不能真正解决您的问题,但可能有助于理解 Storyboard和 ScrollView 的情况。我认为苹果仍然需要解决一些 Storyboard问题。我尝试将 Collection View 与 Storyboard 一起使用,但无法在界面生成器中连接 collectionViewItem(这会在 xibs 中自动发生)。

这是一个 Collection View 的示例:

  1. 将 Collection View 拖放到 View Controller 中。您将看到出现一个 Collection View 和一个collectionViewItem。但 Collection View 项未连接到 Collection View 。如果您使用 IB 尝试此操作,则不会发生任何事情。
  2. 在 IB 的身份检查器中分配一个 Soryboard ID。这是一个随机名称,稍后将在代码中使用。这里我使用“MyCollectionView”
  3. 如果使用 swift,请在模块中选择您的项目名称。 objC 的代码大部分相同
  4. 将 Collection View 连接到包含 Collection View 的 ViewController

进行一些编码以连接 Collection View 项

class IVMyCollectionViewController: NSViewController, NSCollectionViewDelegate {

// manual connections to collection view (which is not working in IB)
@IBOutlet weak var collectionView: NSCollectionView!

class var sharedInstance : IVMyCollectionViewController {

struct Static {

static var instance:IVMyCollectionViewController? = nil
}

if Static.instance != nil {

return Static.instance!
} else {

let bundle = NSBundle.mainBundle()
let infoDict = bundle.infoDictionary!
let sbName = infoDict["NSMainStoryboardFile"] as String
let storyboard = NSStoryboard(name:sbName, bundle:bundle)!
let vcName = "MyCollectionView"
let sbInstance = storyboard.instantiateControllerWithIdentifier(vcName) as IVMyCollectionViewController

Static.instance = sbInstance

return sbInstance
}
}

// implement your controller

}

这意味着某些 UI 元素尚未正确实现。我会向苹果发送错误报告。界面构建器中仍然缺少很多东西。

现在,我将混合使用 Storyboard 和 xib,通过在 Controller 的构造函数中实例化连接,以上述方式滥用 Storyboard 。您可以使用storyboardID 启动 View 和其他 View 或从xib 加载。您可以将 viewController 放置在没有连接(segues)的 Storyboard中,以创建可以像 xibs 一样使用的 View 池。 (viewController 或多或少与 xib 相同)

// objC
DetailViewController* controller = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];

或者像上面一样使用storyboardID进行实例化。

尝试在 Storyboard中创建 ScrollView 。为每个 View 创建您希望在 viewControllers 中显示的 View 。您的 ScrollView View Controller 应该与 ScrollView 本身有连接。在以下示例中, ScrollView 的导出名为“self.contentView”:

// instantiate a view controller for your scrolling view
self.scrollingViewController = [[ScrollingViewController alloc] initWithNibName:@"ScrollingView" bundle:nil];

// or do the same with storyboards by instantiating view controllers by ID
self.scrollingViewController = [myStoryboard instantiateControllerWithIdentifier:@"MyScrollingViewID"];

// Then set the view from the controller as content view
[self.contentView setDocumentView:self.scrollingViewController.view];
[self.contentView.contentView scrollPoint:NSMakePoint(0., self.scrollingViewController.view.frame.size.height)];

这就像混合 Objective C 和 swift 代码一样。苹果似乎已经进入了一条没有走到尽头的转型之路。

一般来说,您应该认为 Storyboard中的 View- 或 WindowControllers 与完整的 xib 文件相同。如果您想使用更多 View ,请在 Storyboard中使用容器 View 。 xibs 中的 FilesOwner 是 Storyboard 中的 viewController。容器 View 使您能够创建附加到 View Controller 的几个 View 。 Segue 机制可用于容器。我认为OS X的 ScrollView 机制并不优雅。我也为此挣扎了很多。

祝你好运!

关于macos - 如何在 NSScrollView OS X (10.10) 中嵌入 Storyboard View Controller 以创建复杂的检查器 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27440433/

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