您的位置:首页 > 移动开发 > Swift

如何在Swift中使用Scene Delegate以编程方式设置您的应用

2020-07-26 12:02 1466 查看

In iOS 12 and before the

AppDelegate
would deal with process level events letting you know when the application launched and terminated and letting you know the state of its UI with UI lifecycle methods like did enter background or foreground, etc. This made the
AppDelegate
the perfect place for developers to configure the
UIWindow
container to programmatically launch the entry point of their application. This was fine in a world of single window applications…

在iOS 12中以及在

AppDelegate
处理进程级事件之前,您可以通过应用程序的启动和终止来了解应用程序的启动状态,并可以使用UI生命周期方法(例如,进入背景或前景等)来了解其UI的状态。这使
AppDelegate
成为了完美的选择开发人员可以将
UIWindow
容器配置为以编程方式启动其应用程序的入口点。 在单窗口应用程序的世界中,这很好。

In iOS 13 though we now have the concept of multi-window session capabilities allowing you to have multiple app instances running at the same time with varying content. Windows are managed by a scene or a

UISceneSession
class. Your app delegate will still handle process events but will no longer be responsible to configure the window anymore. UI lifecycle events are now managed by a
UISceneDelegate
in a different place. This makes the App Delegate more lean and gives your programmatic entry point a more appropriate home where it can receive UI lifecycle methods on a per session basis.

尽管在iOS 13中,我们现在有了多窗口会话功能的概念,该功能使您可以同时运行具有不同内容的多个应用程序实例。 Windows由场景或

UISceneSession
类管理。 您的应用程序委托仍将处理流程事件,但不再负责配置窗口。 UI生命周期事件现在由另一个地方的
UISceneDelegate
管理。 这使App Delegate更加精简,并为您的编程入口点提供了一个更合适的主目录,可以在每个会话的基础上接收UI生命周期方法。

Here’s how to migrate the programmatic setup of your app from the App Delegate over to your Scene Delegate and how to break away from the

Main.storyboard
in iOS 13 and onwards.

这是将应用程序的编程设置从App Delegate迁移到Scene Delegate的方法,以及如何在iOS 13及更高

Main.storyboard
中脱离
Main.storyboard

步骤1/4:删除Main.storyboard文件 (Step 1/4: Remove the Main.storyboard file)

You’ll want to remove the

Main.storyboard
and get rid of the
ViewController.swift
as you’ll most likely want to create your own initial view controller. At the very least you can just rename that class to something more relevant to your app.

您将希望删除

Main.storyboard
并摆脱
ViewController.swift
因为您很可能希望创建自己的初始视图控制器。 至少您可以将该类重命名为与您的应用更相关的名称。

步骤2/4:从info.plist中删除2个键值对 (Step 2/4: Remove 2 key value pairs in info.plist)

You’ll want to go ahead and delete the following key value pairs in your

info.plist
file:1.
Storyboard Name
2.
Main storyboard file base name
and in AppleScript terminology they’re named
UISceneStoryboardFile
and
UIMainStoryboardFile
respectively

您将要继续,并在

info.plist
文件中删除以下键值对:1。
Storyboard Name
2.
Main storyboard file base name
并在AppleScript术语中分别命名为
UISceneStoryboardFile
UIMainStoryboardFile

If you run the app now you’ll see a black screen. This is correct behaviour as you haven’t created a window to be presented in which you’ll also need to create an initial view controller to be displayed in. Which brings us to the next 2 steps. Creating the view controller and creating the window.

如果您现在运行该应用程序,将会看到黑屏。 这是正确的行为,因为您还没有创建要显示的窗口,还需要在其中创建一个初始视图控制器以显示在其中。这将使我们进入接下来的2个步骤。 创建视图控制器并创建窗口。

步骤3/4:创建初始视图控制器 (Step 3/4: Create your initial view controller)

We now have to create the initial view controller that we want to present to the user when they your application. You can have as a complex hierarchy as you like but for this example we will stick to a simple

UIViewController
named
ArticleListViewController
.

现在,我们必须创建要在用户应用程序时呈现给用户的初始视图控制器。 您可以根据需要拥有复杂的层次结构,但是在本示例中,我们将坚持使用名为

ArticleListViewController
的简单
UIViewController

You can create your view controller however you like, in our case it’s as simple as

File -> New -> File …,
selecting a
Cocoa Touch Class
write the name of your class, click next, and hit Create.

您可以根据自己的喜好创建视图控制器,在本例中,它就像

File -> New -> File …,
一样简单
File -> New -> File …,
选择一个
Cocoa Touch Class
输入您的类的名称,单击下一步,然后单击Create。

Your programmatic

UIViewController
class will appear in the project navigator in the file list.

您的程序化

UIViewController
类将出现在文件列表中的项目导航器中。

Note: The view in view controllers in storyboards have a default white background color. This is in comparison to programmatic view controllers whose views do not have a default background color! If you don’t set the background color of your view you’ll end up with a black window at the end of this tutorial. So set the color of your view of your view controller like so

view.backgroundColor = .white
in the
viewDidLoad
method.

注意:情节提要中的视图控制器中的视图具有默认的白色背景色。 这与视图没有默认背景色的程序化视图控制器相比! 如果不设置视图的背景色,则在本教程末尾将出现一个黑色窗口。 因此,请在

viewDidLoad
方法中像
view.backgroundColor = .white
一样设置视图控制器的视图颜色。

最后一步:以编程方式创建窗口层次结构 (Final Step: Programmatically create your window hierarchy)

As developers previously would have written in the app delegate, you now just specify the window setup in the scene delegate.

正如开发人员以前在应用程序委托中编写的那样,您现在只需在场景委托中指定窗口设置。

Run the app and you’ll now have your first scene session managing your the entry point of your application that too programmatically!

运行该应用程序,现在您将拥有第一个场景会话,可以通过编程方式来管理您的应用程序的入口点!

Next time we’ll focus on how to amend your app to create a multi-window application!

下次,我们将重点介绍如何修改您的应用程序以创建多窗口应用程序!

Find out how to unwrap optionals the quick way in another article I wrote!

在我写的另一篇文章中 了解如何快速解开可选 内容

Consider following me here on Medium if you liked my content today as I would love for you to be part of my iOS writing journey.

如果您今天喜欢我的内容,请考虑在Medium上关注我,因为我希望您成为我iOS写作之旅的一部分。

You can check me out on Github and Twitter too.

您也可以在Github和T witter上查看我的信息。

翻译自: https://medium.com/@PavanKataria/how-to-programmatically-setup-your-app-with-scene-delegate-in-swift-b0aab1949b

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐