在NSView中以编程方式创建NSScrollView-Cocoa [英] Create NSScrollView Programmatically in an NSView - Cocoa

查看:190
本文介绍了在NSView中以编程方式创建NSScrollView-Cocoa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSView类,该类负责处理在nib文件中创建的自定义视图.

I have an NSView class which takes care of a Custom View created in the nib file.

现在,我想将NSScrollView添加到自定义视图中,但是我需要以编程方式而不是使用Interface Builder(嵌入到Scroll View中)进行操作.

Now I want to add an NSScrollView to the custom view, but I need to do it programmatically and not using Interface Builder (Embed Into Scroll View).

我找到了以下代码:

NSView *windowContentView = [mainWindow contentView];
NSRect windowContentBounds = [windowContentView bounds];
scrollView = [[NSScrollView alloc] init];
[scrollView setBorderType:NSNoBorder];
[scrollView setHasVerticalScroller:YES];
[scrollView setBounds: windowContentBounds];
[windowContentView addSubview:scrollView];

假设我将上面的变量"mainWindow"和"scrollView"声明为IBOutlet,那么如何将它们连接到Interface Builder中的适当组件?以这种方式这样做有意义吗?

Assuming I declare as IBOutlets the variables 'mainWindow' and 'scrollView' above, how would I go about connecting them to the proper components in Interface Builder? Does it make any sense to do it this way?

还是有更好的方法以编程方式添加滚动视图?

Or is there a better way to add a scroll view programmatically?

P.S.我无法以通常的方式连接它们,因为无法从Interface Builder创建NSObject对象,也无法使用文件所有者.

P.S. I cannot connect them in the usual way because I cannot create an NSObject Object from Interface Builder, or use the File Owner..

推荐答案

此代码片段应演示如何以编程方式创建NSScrollView并使用它显示来自笔尖或代码的任何视图.对于生成的笔尖视图,您只需要事先将笔尖文件加载到自定义视图,并向文件所有者创建自定义视图的出口(outletToCustomViewLoadedFromNib).

This code fragment should demonstrate how to create an NSScrollView programmatically and use it to display any view, whether from a nib or from code. In the case of a nib generated view, you simply need to load the nib file to your custom view prior, and have an outlet to your custom view (outletToCustomViewLoadedFromNib) made to File's Owner.

NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:[[mainWindow contentView] frame]];

// configure the scroll view
[scrollView setBorderType:NSNoBorder];
[scrollView setHasVerticalScroller:YES];

// embed your custom view in the scroll view
[scrollView setDocumentView:outletToCustomViewLoadedFromNib];

// set the scroll view as the content view of your window
[mainWindow setContentView:scrollView];

Apple有一个关于该主题的指南,我不会链接到该指南,因为它需要访问Apple Developer Connection,并且它们的链接经常断开.它的标题为创建和配置滚动视图",当前可以通过使用Google搜索其标题来找到.

Apple has a guide on the subject, which I won't link to as it requires Apple Developer Connection access and their links frequently break. It is titled "Creating and Configuring a Scroll View" and can currently be found by searching for its title using Google.

这篇关于在NSView中以编程方式创建NSScrollView-Cocoa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆