简体   繁体   English

创建完全自定义的NSAlert

[英]Creating a fully customized NSAlert

Is it possible to create a fully customized alert? 是否可以创建完全自定义的警报? I'm doing it with custom sheets now, but I'd like to have the feature that the sheet is blocking (like -[NSAlert runModal] ). 我现在正在使用自定义工作表,但我希望工作表具有阻止功能(例如-[NSAlert runModal] )。

I just want to change the background, really, and the text color, of course. 当然,我只是想改变背景,真的和文字颜色。

Warning about the recommended solution: 关于推荐解决方案的警告:

This code causes wasteful and pointless overhead: 此代码会导致浪费且毫无意义的开销:

for (;;) {
    if ([NSApp runModalSession:session] != NSRunContinuesResponse)
        break;
}

This code is copied straight from the Apple documentation page - but it's meant to show the developer where meaningful code can be inserted for background execution while the modal runs. 此代码直接从Apple文档页面复制 - 但它的目的是向开发人员显示可以在模式运行时插入有意义的代码以供后台执行 That is, you should have some code between the break and the closing bracket. 也就是说,你应该在break和closing括号之间有一些代码。 But there's no actual code shown in the example - and running it like this simply causes your application to poll the session repeatedly until it ends. 但是示例中没有显示实际代码 - 并且像这样运行它只会导致应用程序重复轮询会话直到它结束。 It's like the two-year-old in the back seat of the car on a road trip asking, "Are we there yet? Are we there yet? Are we there yet?..." 这就像在公路旅行中坐在汽车后座上的两岁孩子一样,问道:“我们还在吗?我们还在吗?我们还在吗?...”

If you just want straightforward modal execution, where your application presents a modal window and suspends processing of main / background windows until the modal ends, use this: 如果你只是想要简单的模态执行,你的应用程序呈现模态窗口并暂停处理主/背景窗口直到模态结束,请使用:

[NSApp runModalForWindow: self.window];

...and then exit the modal session when the window closes by dropping this into your window controller subclass: ...然后在窗口关闭时退出模态会话,将其放入窗口控制器子类中:

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

You will need a custom window with custom view drawing, however NSAlert does not allow you to change its window. 您将需要一个带有自定义视图绘制的自定义窗口,但NSAlert不允许您更改其窗口。 So you will need to write your own window controller subclass like NSAlert ( though NSAlert is a subclass of NSObject ). 因此,您需要编写自己的窗口控制器子类,如NSAlert (尽管NSAlertNSObject的子类)。

I looked around a bit, and found this piece of code : 我环顾四周,找到了这段代码:

NSModalSession session = [NSApp beginModalSessionForWindow:sheetWindow];
for (;;) {
    if ([NSApp runModalSession:session] != NSRunContinuesResponse)
        break;
}
[NSApp endModalSession:session];

I call 我打电话

[NSApp stopModal]

to end the session. 结束会议。 Now my code is way cleaner :) 现在我的代码更清洁:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM