MacOS 开发

来源:互联网 发布:扎克拉文数据 编辑:程序博客网 时间:2024/05/22 03:26

  • 一经典用法
    • 创建 Window 和 WindowController
    • 跳转
    • 返回
  • 二模态窗口及关闭方法
    • 监听 NSWindowWillCloseNotification
    • 1Modal windows
    • 2Modal sessions
  • 参考资料


一、经典用法

思路:使用 xib 来创建 NSWindowController,并让后续的调用都继承自这个类。不使用 storyboard。

1、创建项目时,不勾选 Use Storyboards 选项;
创建出来的项目就包含 MainMenu.xib

创建 Window 和 WindowController

2、创建 MainWindow,继承自 NSWindow.

3、创建 MainWindowController 继承自 NSWindowController,勾选 Also create XIB file for user interface。

4、创建好3以后,在MainWindowController.xib中选中Window,将它的继承类改成我们前面2中自定义的BaseWindow。

5、在AppDelegate.m文件类中删除 @property (weak) IBOutlet NSWindow *window;代码,

6、并在MainMenu.xib中选中Window,删除该Window。

7、在AppDelegate.h文件中引用MainWindowController.h文件创建其对象。注意写在 .h 而非 .m 里面,方便后续其他文件调用。

@interface AppDelegate ()@property (strong) MainWindowController *mainWindowC;@end

8、在AppDelegate.m中初始化 window,并让 window 显示和居中。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {    [self initMainWindow];}- (void)initMainWindow{    _mainWindowC = [[MainWindowController alloc]initWithWindowNibName:@"MainWindowController"];    [[_mainWindowC window] center];    [_mainWindowC.window orderFront:nil];}

跳转

9、测试跳转方法,我们另外创建一个窗口 FirstWindowController 继承自NSWindowController ,勾选 xib,并将他的 window 继承自 BaseWindow。

10、给 MainWindowController.xib 拖拽按钮,并将按钮方法连线到 MainWindowController.m 中。

11、在 windowDidLoad 中初始化 firstWindow,并在按钮点击方法中编写跳转方法。

- (void)windowDidLoad {    [super windowDidLoad];    self.contentViewController.view.wantsLayer = YES;    self.contentViewController.view.layer.backgroundColor = [NSColor magentaColor].CGColor;    self.firstWindowC = [[FirstWindowController alloc]initWithWindowNibName:@"FirstWindowController"];}- (IBAction)btn1OnClick:(id)sender {    [self.firstWindowC.window orderFront:nil];//显示要跳出来的窗口    [self.window orderOut:nil];//关闭当前窗口}

返回

12、为了测试从 firstWindow 返回到 mainWindow,放置按钮到 FirstWindowController.xib,并给按钮拉线写方法:

- (IBAction)btn1OnClick:(id)sender {    AppDelegate *adelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];    [self.window close];    [[adelegate.mainWindowC window] makeKeyAndOrderFront:nil];}

二、模态窗口及关闭方法

有2种创建模态窗口的方法
可设置属性来区别两种弹出方式:

@property(nonatomic,assign)NSModalSession sessionCode;

监听 NSWindowWillCloseNotification

这两种方法的结束,都需要在调用的界面的 windowDidLoad 中添加监听:

    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(windowWillClose:)                                                 name:NSWindowWillCloseNotification                                               object:nil];

1、Modal windows

特性:这种window比较霸道,当它启动后,此时只有这个window可以接收响应用户操作,无法切换到其他窗口操作,其他窗口也不能接收处理系统内部的各种事件。

调用方法:

-(IBAction)showModelWindowAction:(id)sender {    _sessionCode = 0; //防止 session 模式弹出窗口后,sessionCode 没有更改。    NSLog(@"_sessionCode2 : %d",_sessionCode);    [[NSApplication sharedApplication]runModalForWindow:self.firstWindowC.window];}

结束方法:

- (void)windowWillClose:(NSNotification *)notification {    [[NSApplication sharedApplication] stopModal];}

2、Modal sessions

比起Modal windows,Modal sessions方式创建的window稍微温和一些,允许响应快捷键和系统菜单,比如字体颜色选择这些panel面板.

启动Modal sessions 窗口

- (IBAction)showSessionsWindow:(id)sender {    _sessionCode = [[NSApplication sharedApplication]beginModalSessionForWindow:self.firstWindowC.window];    NSLog(@"_sessionCode3 : %d",_sessionCode);}

关闭窗口

- (void)windowWillClose:(NSNotification *)notification {    [[NSApplication sharedApplication] stopModal];    NSLog(@"_sessionCode00 : %d",_sessionCode);    if (_sessionCode != 0) {        [[NSApplication sharedApplication]endModalSession:_sessionCode];    }}

注意任何一种模态窗口,都必须调用结束模态的方法stopModal去结束模态。
如果点击了window左上角的关闭按钮,而没有执行结束模态的方法。整个系统仍然处于模态,其他窗口无法正常工作。


参考资料:

mac app开发之:利用NSWindowController实现窗口跳转 -
http://blog.csdn.net/y_zhangpengwei/article/details/50817132

DIOS2012:http://www.jianshu.com/p/017f147d3b2d

原创粉丝点击