有关NSApp的ModalForWindow,NSAlert的ModalForWindow和ModalSession的提示 [英] Tips on NSApp’s ModalForWindow, NSAlert’s ModalForWindow, and ModalSession

查看:527
本文介绍了有关NSApp的ModalForWindow,NSAlert的ModalForWindow和ModalSession的提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间进行实验,以消除对Objective-C的"ModalForWindow"语言以及随后如何使用模式会话的困惑.也许以下技巧可以节省一些时间:

It took me quite a bit of experimentation to clear up some confusion over Objective-C’s "ModalForWindow" language and, subsequently, how to use a modal session. Maybe the following tips will save somebody some time:

(如果您是这个概念的新手,则当窗口(通常是面板)以模式运行时,它会阻止应用程序的其他部分响应,直到将其关闭为止.)

(In case you’re new to the concept: When a window, usually a panel, runs modal, it prevents some other part of the app from responding until it has been dismissed.)

"ModalForWindow"在不同情况下的含义不同. 如果您正在使用loadNibNamed显示由xib定义的面板,并且希望它以模式运行,则在显示该面板时调用它:

"ModalForWindow" means different things in different circumstances. If you are using loadNibNamed to display a panel defined by a xib and you want it to run modal, call this once it is displayed:

// Make panelReviewImports modal, so that no other part of app will respond.
[[NSApplication sharedApplication] runModalForWindow:self.panelReviewImports];

,并在其解雇方法中对此进行跟进:

and follow up with this in its dismissal methods:

[[NSApplication sharedApplication] stopModal];

但是对于NSAlert而言,beginSheetModalForWindow中的窗口"是指将警报作为一个工作表附加到的窗口,该窗口将被冻结直到警报被解除.但是应用不会被冻结;所有其他窗口将保持可操作状态. 如果您想将警报作为工作表附加,并且 冻结应用程序的其余部分,通过简单调用runModal来遵循beginSheet代码,并显式使用返回代码,如下所示:

But for NSAlert, the "window" in beginSheetModalForWindow refers to the window to which the alert will be attached as a sheet, which window will be frozen until the alert is dismissed. But the app won’t be frozen; all other windows will remain operable. If you want to attach an alert as a sheet and also freeze the rest of the app, follow the beginSheet code with a simple call to runModal and use the return code explicitly, like this:

[alert beginSheetModalForWindow:self.window 
                  modalDelegate:self didEndSelector:@selector(abandonmentAlertDidEnd:returnCode:contextInfo:) 
                    contextInfo:nil];
NSInteger returnCode = [alert runModal];
[self abandonmentAlertDidEnd:alert returnCode:returnCode contextInfo:nil];

(当然,您将实现abandonmentAlertDidEnd:returnCode:contextInfo:代码作为类方法.)

(Of course, you will have implemented the abandonmentAlertDidEnd:returnCode:contextInfo: code as a class method.)

或者,如果您希望警报作为居中面板运行,请自行调用runModal.

Or, if you want the alert to run as a centered panel, call runModal by itself.

假设您要运行面板模式,然后在用户提交无效条目时发出警报.在显示警报之前,您必须先stopModal-之后,由于某种原因,另一个对runModalForWindow的调用将无法正常工作.对于这种情况,您需要一个模式会话:

Suppose you want to run a panel modal, followed by an alert if the user submits an invalid entry. You’d have to stopModal before you show the alert — after which, for some reason, another call to runModalForWindow fails to work properly. For this scenario, you need a modal session:

1)将NSModalSession属性添加到您的控制器类中,因为modalSession必须可以通过多种方法访问.

1) Add an NSModalSession property to your controller class, because the modalSession must be accessible across multiple methods.

2)显示面板后,调用beginModalSessionForWindow实例化modalSession:

2) Once you have displayed the panel, call beginModalSessionForWindow to instantiate the modalSession:

self.modalSession = [[NSApplication sharedApplication] beginModalSessionForWindow:self.panelForInput];

3)接下来是一个调用runModalSession的while循环,当其返回值不等于NSRunContinuesResponse时,它会中断:

3) Follow this up with a while-loop that calls runModalSession, breaking when its return does not equal NSRunContinuesResponse:

while ([[NSApplication sharedApplication] runModalSession:self.modalSession] == NSRunContinuesResponse)
    continue;

当用户单击面板的按钮之一时,循环将中断并且应用程序将释放. (在面板的文本字段中输入将使模式会话保持不变.)

The loop will break and the app will free up when the user clicks on one of the panel’s buttons. (Typing in the panel’s textfield will leave the modal session intact.)

4)在按钮处理中,如果用户输入无效,则使用runModal调用警报.

4) In your button handling, if the user’s entry is invalid, you call an alert with runModal.

5)在警报调用的紧下方,在解除警报后将执行的代码中,将与上面使用的while循环相同.小组的模式会议恢复.

5) Immediately below the alert call, in the code which will execute once the alert is dismissed, you put the same while-loop used above. The panel’s modal session resumes.

6)在进行关闭面板的操作时(无论是有效输入还是取消操作),您都调用endModalSession,奇怪的是,这还不够;即使您从未调用过runModalForWindow,也必须调用stopModal.

6) In your handling to close the panel, either upon valid entry or cancel, you call endModalSession, which, oddly, isn’t enough; you must also call stopModal, even though you never called runModalForWindow.

[[NSApplication sharedApplication] endModalSession:self.modalSession];
[[NSApplication sharedApplication] stopModal];
[self.panelForInput close];

推荐答案

问题就是答案.我只是发布此内容以将其关闭.很抱歉扭曲stackoverflow格式.

The question is the answer. I'm just posting this to close it out. Sorry for twisting the stackoverflow format.

这篇关于有关NSApp的ModalForWindow,NSAlert的ModalForWindow和ModalSession的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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