Skip to content

0.APP结构和行为-App Structure and Behavior

定义应用的入口点和顶层结构。

Define the entry point and top-level organization of your app.

  • 概述

通过创建一个符合App协议的结构来声明你的应用程序,并向它添加一个或多个场景。您可以使用内置的场景,如WindowGroup,或者您可以编写符合场景协议的自定义场景。场景包含定义你的应用程序的用户界面的视图层次结构,并有一个系统管理的生命周期。

Declare your app by creating a structure that conforms to the App protocol and add one or more scenes to it. You can use built-in scenes, like WindowGroup, or you can compose custom scenes that conform to the Scene protocol. Scenes contain the view hierarchies that define the user interface of your app, and have a life cycle that the system manages.

通过应用CommandMenu实例向场景添加菜单命令。你也可以使用CommandGroup更新系统提供的菜单。当你为命令提供键盘快捷键时,iOS、iPadOS和tvOS会将这些快捷键作为按键命令使用。

Add menu commands to a scene by applying CommandMenu instances. You can also update system-provided menus using a CommandGroup. When you provide keyboard shortcuts for commands, iOS, iPadOS, and tvOS make those available as key commands.

你可以配合WidgetKit和SwiftUI为您的应用程序添加widget。Widget提供了对应用程序中相关内容的快速访问。定义一个符合Widget协议的结构,并为widget声明一个视图层次结构。

You can use SwiftUI, working together with WidgetKit, to add widgets to your app. Widgets provide quick access to relevant content from your app. Define a structure that conforms to the Widget protocol, and declare a view hierarchy for the widget.

通过调用动作与系统的其他部分进行交互。例如,你可以用OpenURLAction实例打开一个URL。

Interact with other parts of the system by invoking actions. For example, you can open a URL with an OpenURLAction instance.