learn from:Windows and Window Controllers in OS X Tutorial
Overview
window is the isntance of the NSWindow class, and the associated controller object is an instance of the NSWindowController class.In a well-designed app,you typically see a one-to-one relationship between a window and its controller.
Learn What
we’ll learn about:
- Windows and window controllers
- The Document architecture
- NSTextView
- Modal windows
- The menu bar and menu items
Document Architecture
创建工程的时候,勾选Create Document-Based Application.
A document is a container for data in memory that you can view in a window.Eventually,it can be written to or read from a disk or iCloud.
- NSDocument: Creates,presents and stores document data
- NSWindowController:Manages a window in which a document is displayed
- NSDocumentController: manages all of the document objects in the app
关系如图所示(Picture from博客):
(图来自作者Gabriel Miro)
In Document.m
// for writing
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
[NSException raise:@"UnimplementedMethod" format:@"%@ is unimplemented", NSStringFromSelector(_cmd)];
return nil;
}
// for read
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
[NSException raise:@"UnimplementedMethod" format:@"%@ is unimplemented", NSStringFromSelector(_cmd)];
return YES;
}
保存与打开document不在本教程范围
Window Position
Note: 在OS X中,坐标体系跟iOS有些许区别,采取的是(x,y)左下角原点(0,0)坐标,即垂直方向的值从上到下是递减的,而iOS中是左上角原点坐标(0,0),y方向的值从上到下是递增的。
在OS X中,如果想要看到窗口初始位置的修改,不仅需要rebuild跟run,还需要关闭之前的窗口打开新窗口。
visibleFrame excludes the areas taken by the dock and menu bar. If you don’t take this into account, you might end up with the dock obscuring part of your window.
When you enable dock and menu hiding, visibleFrame may still be smaller than frame, because the system retains a small boundary area to detect when to show the dock.
visibleFrame与frame: visibleFrame不包括dock和menu bar.frame的坐标体系是有将dock和menu bar计算在内。所以当存在dock或者menu bar的时候,visibleFrame的坐标会比frame的坐标小。但当关闭dock和menu bar的时候,visibleFrame 仍有可能会比frame小,原因在于系统仍旧保留的了一个小的区域显示dock。
NSWindowController has a window property and NSWindow has a screen property. You use these two properties to access the geometry of the window and the screen.
NSWindowController有window属性,NSWindow有一个screen属性。通过这两个属性就可以获取到window和screen的坐标体系。
Note: If you’re an iOS developer, please note that in Cocoa, NSWindow is NOT a subclass of NSView. In iOS, UIWindow is a special subclass, of UIView. UIWindow itself is the root of the view hierarchy, and it’s simply playing the role of the content view.
在iOS中UIWindow继承于UIView,但在OS X中,NSWindow不是继承于NSView,而是NSResponder.
Enable the Font Panel
Format-Font-show Fonts,Ctrol拖拉与First Responder绑定事件,选择orderFrontFontPanel。
Modal Windows
获取window demo
NSStoryboard *mainStoryboard = [NSStoryboard storyboardWithName:@"Main" bundle:nil];
NSWindowController *windowController = [mainStoryboard instantiateControllerWithIdentifier:@"storyboardID"]; // 在windowController中设置storyboardID
NSWindow *window = windowController.window;
// 可在CustomViewController中对内容做一定的定制
CustomViewController *customViewController = window.contentViewController;
// show Modal
[[NSApplication sharedApplication] runModalForWindow:wordCountWindow];
// dismiss
[NSApplication sharedApplication] stopModal];
弹出新窗口
// show
[mainWindow addChildWindow: self.customWindow ordered: NSWindowAbove];
// dismiss
[mainWindow removeChildWindow: self.customWindow];
// 如果不设置nil有个bug,显示弹窗口,设置了过0.5s就自动消失,使用的是performSelector:withObject:afterDelay, bug是任意点击下界面才会消失,而不是自动消失的效果,(r: 目前还不知道是延时操作这边导致的还是其它)
self.customWindow = nil;