macOSX使用通知实现窗口切换

来源:互联网 发布:算法导论 kindle mobi 编辑:程序博客网 时间:2024/06/05 13:32

实现功能:在使用usb给app升级过程中会检测USB是否拔掉,当USB拔掉时,要弹出错误提示窗口,提醒再次升级,点击OK button后回到重新升级页面,如下图:
这里写图片描述

采用发通知的方式实现:

总体思路:在主窗口新建一个NSNotificationCenter来监控消息,当USB拔掉,跳到错误提示窗口,在点击OK buton后,发通知,主窗口接收到通知执行相应的函数操作。

主窗口操作:在viewDidLoad中(即主窗口加载时)新建一个NSNotificationCenter来监控消息,如果接受到消息就执行ErrorNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ErrorNotification) name:@”errorupdate” object:nil];
升级过程操作:在升级中,如果检测到USB拔掉了,就跳到错误窗口
_ErrorWindow = [[ErrorWindowController alloc] initWithWindowNibName:@”ErrorWindowController”];

[_ErrorWindow.window orderFront:nil]; //显示需要跳转的窗口

错误提示窗口操作:当点击OK button就会新建一个通知,然后发送通知,关闭错误提示窗口。
NSNotification *notification =[NSNotification notificationWithName:@”errorupdate” object:nil userInfo:nil]; //新建通知
[[NSNotificationCenter defaultCenter] postNotification:notification];//通过通知中心发送通知
[self.window close];//关闭error提示窗口