xcode 如何在 Mac OS 中创建模态滑出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32426294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create modal slide-out window in Mac OS?
提问by Vasily
回答by ElmerCat
That kind of modal window is called a Sheet. It's very easy to get this behavior with a Storyboard segue, or programmatically with an NSViewController
subclass. The example below is just a blank OS X Cocoa application as created by Xcode. (I chose Swift as the language, but it will work the same way with Objective-C.)
那种模态窗口称为工作表。使用 Storyboard segue 或以编程方式使用NSViewController
子类很容易获得这种行为。下面的示例只是一个由 Xcode 创建的空白 OS X Cocoa 应用程序。(我选择 Swift 作为语言,但它与 Objective-C 的工作方式相同。)
The only things I added to the storyboard was a second View Controller for the sheet view, and a label and pushbutton on each view.
我添加到故事板的唯一内容是用于工作表视图的第二个视图控制器,以及每个视图上的标签和按钮。
Displaying The Sheet View With A Storyboard Segue
使用 Storyboard Segue 显示工作表视图
With the Sheet View controller selected and the Connections Inspector tab displayed, connect "Presenting Segues - sheet" to the "Display Sheet" button.
选中 Sheet View 控制器并显示 Connections Inspector 选项卡后,将“Presenting Segues - sheet”连接到“Display Sheet”按钮。
Connect "Received Actions - dismissController:" to the "Close Sheet" button.
将“Received Actions - deniedController:”连接到“Close Sheet”按钮。
That's it! There's no code needed to make this example work; just build and run.
就是这样!无需代码即可使此示例正常工作;只需构建和运行。
Displaying The Sheet View Programmatically
以编程方式显示图纸视图
Note that Xcode creates the default project with two custom class files. In the Storyboard, AppDelegate.swiftis represented in the Application scene:
请注意,Xcode 使用两个自定义类文件创建默认项目。在 Storyboard 中,AppDelegate.swift表示在 Application 场景中:
We don't need to use the AppDelegate for this example, but you could use it for interaction with the Main Menu, or other things.
我们不需要在这个例子中使用 AppDelegate,但是你可以用它来与主菜单或其他东西进行交互。
The custom ViewController.swiftcustom class will be used to present the sheet. It is represented in the View Controller scene:
自定义ViewController.swift自定义类将用于呈现工作表。它在 View Controller 场景中表示:
To instantiate the Sheet View Controller programmatically, it needs a Storyboard ID. Here, we'll give it the ID "SheetViewController". Note that it's still a plain NSViewController
; we don't need to make it a custom class for this example, but your application might want to:
要以编程方式实例化 Sheet View Controller,它需要一个 Storyboard ID。在这里,我们将为其指定 ID“ SheetViewController”。请注意,它仍然是一个普通的NSViewController
; 对于此示例,我们不需要将其设为自定义类,但您的应用程序可能希望:
Displaying the ViewController.swiftfile in the assistant editor, Ctrl-drag a connection from the "Display Sheet" button into the custom class. This will create stub code for an @IBAction function we'll name "displaySheet":
在助手编辑器中显示ViewController.swift文件,按住 Ctrl 键将连接从“显示表”按钮拖到自定义类中。这将为@IBAction 函数创建存根代码,我们将其命名为“displaySheet”:
In the ViewController.swiftfile, we'll implement the Sheet View Controller as a lazy var. It will get instantiated only once, the first time it's accessed. That will happen the first time the displaySheet function is called.
在ViewController.swift文件中,我们将 Sheet View Controller 实现为一个惰性变量。它只会在第一次访问时实例化一次。这将在第一次调用 displaySheet 函数时发生。
// ViewController.swift
import Cocoa
class ViewController: NSViewController {
lazy var sheetViewController: NSViewController = {
return self.storyboard!.instantiateControllerWithIdentifier("SheetViewController")
as! NSViewController
}()
@IBAction func displaySheet(sender: AnyObject) {
self.presentViewControllerAsSheet(sheetViewController)
}
}
Swift 4 version:
斯威夫特 4 版本:
// ViewController.swift
import Cocoa
class ViewController: NSViewController {
lazy var sheetViewController: NSViewController = {
return self.storyboard!.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "SheetViewController"))
as! NSViewController
}()
@IBAction func displaySheet(sender: AnyObject) {
self.presentViewControllerAsSheet(sheetViewController)
}
}
As in the first example, the "Close Sheet" button is connected to the "dismissController:" action on the Sheet View Controller. Alternatively, you could call that function programmatically from your ViewController class:
在第一个示例中,“关闭工作表”按钮连接到工作表视图控制器上的“dismissController:”操作。或者,您可以从 ViewController 类中以编程方式调用该函数:
self.dismissController(sheetViewController)
For more information, refer to the Apple "Sheets Programming Topics" document: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Sheets/Sheets.html
更多信息请参考苹果“Sheets Programming Topics”文档:https: //developer.apple.com/library/content/documentation/Cocoa/Conceptual/Sheets/Sheets.html
回答by HaiderSahib
Objective-C version:
Objective-C 版本:
- (IBAction)displaySheet:(id)sender {
NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Main" bundle: nil];
NSViewController * vc = [storyboard instantiateControllerWithIdentifier:@"SheetViewController"];
[self presentViewControllerAsSheet:vc];
}