如何在Swift中使用Scene Delegate以编程方式设置您的应用
In iOS 12 and before the
AppDelegatewould 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
AppDelegatethe perfect place for developers to configure the
UIWindowcontainer 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
UISceneSessionclass. 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
UISceneDelegatein 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.storyboardin 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.storyboardand get rid of the
ViewController.swiftas 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.plistfile:1.
Storyboard Name2.
Main storyboard file base nameand in AppleScript terminology they’re named
UISceneStoryboardFileand
UIMainStoryboardFilerespectively
您将要继续,并在
info.plist文件中删除以下键值对:1。
Storyboard Name2.
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
UIViewControllernamed
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 Classwrite the name of your class, click next, and hit Create.
您可以根据自己的喜好创建视图控制器,在本例中,它就像
File -> New -> File …,一样简单
File -> New -> File …,选择一个
Cocoa Touch Class输入您的类的名称,单击下一步,然后单击Create。
Your programmatic
UIViewControllerclass 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 = .whitein the
viewDidLoadmethod.
注意:情节提要中的视图控制器中的视图具有默认的白色背景色。 这与视图没有默认背景色的程序化视图控制器相比! 如果不设置视图的背景色,则在本教程末尾将出现一个黑色窗口。 因此,请在
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.
- 如何使用WebBrowser控件打印格式化的XML文档,并以编程方式任意设置打印方向(C#完整示例)
- 如何使用Rexsee以Web开发方式创建Android应用
- 使用iis设置虚拟主机(如何用同一端口ip对应不同web应用)
- 如何使用 ADO.NET 和 Visual C++ .NET 以编程方式创建 SQL Server 数据库
- C# vs中应用设置变量如何绑定及代码中如何使用
- [摘自MSDN] ASP.Net2.0学习 [2] 主题 6 :如何:以编程方式应用 ASP.NET 主题
- ORACLE如何在有视图的SQL中使用hint(提示)来设置视图内的SQL查询方式
- VISTA或WIN7下使用立体声混音作为录音来源时,如何通过编程方式去除或加入麦克风的输入
- [Swift]以函数式编程的方式使用字符串中的Ranges
- react-native开发swift的ios应用,使用callback通信方式回调键值对组
- parcel react_如何使用Parcel设置React应用
- 如何在 Internet Explorer 中以编程方式更改 Internet Explorer 和 WebBrowser 控件的打印设置
- 关于本地管理的Extent分配使用 Uniform 方式时,如何设置这个数字
- 如何在运行 Windows XP Service Pack 2 的系统上使用脚本以编程方式打开端口供 SQL Server 使用
- 如何编程方式设置301跳转?
- Spring如何设置让事务自动提交和回滚?Spring两种事务管理方式的配置及使用
- Redis简介、与memcached比较、存储方式、应用场景、生产经验教训、安全设置、key的建议、安装和常用数据类型介绍、ServiceStack.Redis使用(1)
- 编程技巧:在Java应用开发中如何使用线程
- ORACLE如何在有视图的SQL中使用hint(提示)来设置视图内的SQL查询方式
- 学习笔记(02):Python 面试100讲(基于Python3.x)-设置Python模块搜索路径有几种方式,如何使用...