NSWindowController上的viewWillAppear或viewDidAppear [英] viewWillAppear or viewDidAppear on NSWindowController

查看:103
本文介绍了NSWindowController上的viewWillAppear或viewDidAppear的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode5.1在MacOS X上开发应用程序

I'm developing an app on MacOS X with Xcode5.1

有一个我想在用户每次打开或显示NSWindowController时触发的动作 我发现的只是

and there's an action I want to trigger everytime the user opens or shows a NSWindowController all I found was

  • windowDidLoad
  • windowWillLoad
  • awakeFromNib

但与iOS中的东西不同:我的方法...

but nothing like in iOS: my methods...

  • viewWillAppear
  • viewDidAppear

因为即使我用

[NSWindowController close];

[NSWindowController close];

如果我再次打开它,它不会从windowDidLoad,windowDidAppear或awakeFromNib触发我的动作

if I open it again, it doesn't trigger my actions from windowDidLoad, windowDidAppear or awakeFromNib

现在我需要类似它们的东西,这是等效的东西,一定是某些东西

and now I need something like them, what's the equivalent, it must be something

提前感谢您的支持

推荐答案

是的,NSWindowController中没有这样方便的方法.让我解释一下原因. iOS视图控制器和OS X窗口控制器之间存在区别:在iOS中,视图控制器可以全屏显示或完全隐藏在屏幕之外.就这样. OS X中的窗口具有更多自由度:可以显示,隐藏,调整大小,最小化/还原,由其他应用程序的窗口覆盖,全屏显示,转到另一个屏幕(在多显示器配置中)等.在此活动中,NSWindow具有一个委托(该委托会自动映射到xib中相应的NSWindowController上).看看 NSWindowDelegate 文档.因此,iOS出现"和OS X动作之间没有直接的行为映射.但是我们可以尝试使用最近的可能事件.

Yes, there's no such convenient methods in NSWindowController. Let me explain why.
There is a difference between iOS view controller and OS X window controller: in iOS, view controller can appear fullscreen or completely hide from screen. That's all. The window in OS X has many more degrees of freedom: it can be shown, hidden, resized, minimized/restored, overlayed by other apps' windows, go fullscreen, go to another screen (in multimonitor configuration), etc. For tracking all this activity, NSWindow has a delegate (which is automatically mapped on corresponding NSWindowController in xib). Take a look at NSWindowDelegate docs. So there's no direct behavioral mapping between iOS "appear" and OS X bunch of actions. But we can try to use the nearest possible events.

对于您的情况(使窗口中的内容可见),我可以提供2种不同的方法.
首先,在您的NSWindowController子类中覆盖showWindow动作:

For your case (do something at window become visible), I can offer 2 different methods.
First, override the showWindow action in your NSWindowController subclass:

- (IBAction)showWindow:(id)sender
{
    [super showWindow:sender];

    // your code here
}

这样,每次创建/显示窗口时都会调用您的代码.

This way, your code will be called every time window is created/shown on screen.

或者第二,使用委托方法:

Or second, use the delegate method:

- (void)windowDidChangeOcclusionState:(NSNotification *)notification
{
    // notification.object is the window that changed its state.
    // It's safe to use self.window instead if you don't assign one delegate to many windows
    NSWindow *window = notification.object;

    // check occlusion binary flag
    if (window.occlusionState & NSWindowOcclusionStateVisible)  
    {
        // your code here
    }
}

这样,每当窗口(或其一部分)可见时,就会调用您的代码.例如,如果用户最小化了位于您的窗口上方的另一个窗口(或将其移至某处),则可能发生此事件.当您想在不可见的窗口中暂停动画/计时器/等以节省一些CPU时,通常是这样的:)
如果您需要在窗口消失上做某事(例如,启用了hidesOnDeactivate标志的窗口未关闭且不会调用相应的委托方法;它们只是从中删除了),这也是一种非常有用的方法.屏幕而不关闭).这种方法使我们可以跟踪这些情况:

This way, your code will be called every time when window (or it's part) will become visible. For example, this event can occur if user minimized the other window that was over your window (or moved it somewhere). It is usual when you want to suspend animation/timers/etc in invisible window to save some cpu :)
It's also very useful method if you need to do something on window disappear (for example, windows with enabled hidesOnDeactivate flag is not closed and does not call corresponding delegate methods; they just removed from screen without closing). This method allow us to track those situations:

- (void)windowDidChangeOcclusionState:(NSNotification *)notification
{
    if (self.window.occlusionState & NSWindowOcclusionStateVisible)
    {
        // Appear code here
    }
    else
    {
        // Disappear code here
    }
}

这篇关于NSWindowController上的viewWillAppear或viewDidAppear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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