Presenting a View Controller                                                 --显示vc

There are two ways to display a view controller onscreen: embed it in a container view controller or present it. Container view controllers provide an app’s primary navigation, but presenting view controllers is also an important navigation tool. You use direct presentation to display a new view controller on top of the current one. Typically, you present view controllers when you want to implement modal interfaces, but you can also use them for other purposes.

--在屏幕上显示vc有两种方法:将其嵌入容器vc或直接显示它。容器vc提供应用程序的主导航,但“显示vc”也是一个重要的导航工具。使用“直接显示”可以在当前视图控制器的基础上显示新的vc。通常,当您想要实现模式接口时,您会使用显示vc,当然显示vc也可用作其他用途。

Support for presenting view controllers is built in to the UIViewController class and is available to all view controller objects. You can present any view controller from any other view controller, although UIKit might reroute the request to a different view controller. Presenting a view controller creates a relationship between the original view controller, known as the presenting view controller, and the new view controller to be displayed, known as the presented view controller. This relationship forms part of the view controller hierarchy and remains in place until the presented view controller is dismissed.

--对显示vc的支持内置于UIViewController类中,可用于所有vc对象。您可以在任何vc中显示vc,尽管UIKit可能会将“请求”重新路由到其他vc。显示vc会在原始vc(称为 在显示的vc)和将要显示的新vc(称为被显示vc)之间创建关系。此关系构成vc层次结构的一部分,并在解除所被显示vc之前保持不变

The Presentation and Transition Process                                            --演示和转换过程

Presenting a view controller is a quick and easy way to animate new content onto the screen. The presentation machinery built into UIKit lets you display a new view controller using built-in or custom animations. The built-in presentations and animations require very little code because UIKit handles all of the work. You can also create custom presentations and animations with little extra effort and use them with any of your view controllers.

--演示vc是在屏幕上设置新内容动画的一种快速而简单的方法。UIKit中内置的演示机制允许您使用内置或自定义的动画显示新的vc。内置的演示和动画只需要很少的代码,因为UIKit处理所有的工作。您还可以轻松创建自定义演示文稿和动画,并将它们与任何vc一起使用。

You can initiate the presentation of a view controller programmatically or using segues. If you know your app’s navigation at design time, segues are the easiest way to initiate presentations. For more dynamic interfaces, or in cases where there is no dedicated control to initiate the segue, use the methods of UIViewController to present your view controllers.

--您可以通过编程或使用segues来启动vc的演示。如果您在设计时知道app的导航,则segue是启动演示的最简单方法。对于更灵活的接口,或者在没有专用控件来启动segue的情况下,可以使用UIViewController里的方法来显示vc。

Presentation Styles                                                                                  --演示的样式

The presentation style of a view controller governs its appearance onscreen. UIKit defines many standard presentation styles, each with a specific appearance and intent. You can also define your own custom presentation styles. When designing your app, choose the presentation style that makes the most sense for what you are trying to do and assign the appropriate constant to the modalPresentationStyle property of the view controller you want to present.

--vc的演示样式控制其在屏幕上的外观。UIKit定义了许多标准的演示样式,每个样式都有特定的外观和意图。您还可以定义自己的自定义演示样式。在设计app时,请选择对app的操作有意义的演示样式,并将恰当的常量赋值给要演示的vc的 modalPresentationStyle 属性。

Full-Screen Presentation Styles                                                                 --全屏显示样式

Full screen presentation styles cover the entire screen, preventing interactions with the underlying content. In a horizontally regular environment, only one of the full-screen styles covers the underlying content completely. The rest incorporate dimming views or transparency to allow portions of the underlying view controller to show through. In a horizontally compact environment, full-screen presentations automatically adapt to the UIModalPresentationFullScreen style and cover all of the underlying content.

--全屏显示样式覆盖了整个屏幕,并阻止与底层内容的交互。在水平规则的环境中,只有一种全屏样式可以完全覆盖底层内容。其余的则包含了暗显视图或透明度,用于将底层vc的某部分显示出来。在水平紧凑的环境中,全屏演示将自动适应UIModalPresentationFullScreen样式并覆盖所有底层内容。

Figure 8-1 illustrates the appearance of presentations using the UIModalPresentationFullScreen, UIModalPresentationPageSheet, and UIModalPresentationFormSheet styles in a horizontally regular environment. In the figure, the green view controller on the top-left presents the blue view controller on the top-right and the results of each presentation style are shown below. For some presentation styles, UIKit inserts a dimming view between the content of the two view controllers.

--图8-1说明了在水平规则环境中使用了UIModalPresentationFullScreen、UIModalPresentationPageSheet和UIModalPresentationFormSheet 演示样式的外观。在图中,左上角的绿色vc显示了右上角的蓝色vc,每个表示样式的结果如下所示。对于某些表示样式,UIKit在两个视图控制器的内容之间插入一个“暗显视图”。

Figure 8-1 The full screen presentation styles                                     --全屏显示样式

Note

When presenting a view controller using the UIModalPresentationFullScreen style, UIKit normally removes the views of the underlying view controller after the transition animations finish. You can prevent the removal of those views by specifying the UIModalPresentationOverFullScreen style instead. You might use that style when the presented view controller has transparent areas that let underlying content show through.

--当使用UIModalPresentationFullScreen样式来演示vc时,UIKit通常会在转换动画完成后移除底层vc的视图。此时 ,作为顶替, 您可以通过使用UIModalPresentationOverFullScreen样式来防止删除这些视图。当被显示的vc具有“为了显示底层内容的”透明区域时,也可以使用该样式。

When using one of the full-screen presentation styles, the view controller that initiates the presentation must itself cover the entire screen. If the presenting view controller does not cover the screen, UIKit walks up the view controller hierarchy until it finds one that does. If it can’t find an intermediate view controller that fills the screen, UIKit uses the root view controller of the window.

--使用全屏显示样式之一时,启动演示的vc必须覆盖整个屏幕。如果在显示的vc没有覆盖屏幕,UIKit会沿着vc层次结构向上移动,直到找到一个覆盖屏幕的vc为止。如果找不到能填充屏幕的中介vc,UIKit将使用窗口的根vc。

The Popover Style                                                                                                 --弹窗的样式

The UIModalPresentationPopover style displays the view controller in a popover view. Popovers are useful for displaying additional information or a list of items related to a focused or selected object. In a horizontally regular environment, the popover view covers only part of the screen, as shown in Figure 8-2. In a horizontally compact environment, popovers adapt to the UIModalPresentationOverFullScreen presentation style by default. A tap outside the popover view dismiss the popover automatically.

--UIModalPresentationPopover样式在popover视图中显示vc。弹窗用于显示与“聚焦或选定对象”相关的额外信息或项目列表。在水平常规环境中,popover视图只覆盖屏幕的一部分,如图8-2所示。在水平紧凑的环境中,默认情况下弹窗遵守UIModalPresentationOverFullScreen样式。手机屏幕上, 在“弹窗视图”范围外 轻按 即可自动关闭弹窗。

Figure 8-2 The popover presentation style                                                                     --弹窗的显示样式

Because popovers adapt to full-screen presentations in a horizontally compact environment, you usually need to modify your popover code to handle the adaptation. In full-screen mode, you need a way to dismiss a presented popover. You can do that by adding a button, embedding the popover in a dismissible container view controller, or changing the adaptation behavior itself.

--因为弹窗遵循 水平紧凑环境中 的全屏演示样式,所以你要修改 ”弹窗的代码“ 来处理 “环境的适应”。在全屏模式下,您需要一种方法来解除被显示的弹窗。您可以通过添加按钮、将弹窗嵌入可解除的容器vc、或更改弹窗本身的自适应行为来完成此解除操作。

For tips on how to configure a popover presentation, see Presenting a View Controller in a Popover.

--有关如何配置弹出窗口表示的技巧,请参见超链接: “在弹出窗口中显示视图控制器”。

The Current Context Styles                                                                     --当前上下文的样式

The UIModalPresentationCurrentContext style covers a specific view controller in your interface. When using the contextual style, you designate which view controller you want to cover by setting its definesPresentationContext property to YES. Figure 8-3 illustrates a current context presentation that covers only one child view controller of a split view controller.

--UIModalPresentationCurrentContext样式涵盖界面中的特定vc。使用上下文样式时,将该vc的definesPresentationContext 属性设置为“YES”, 就是指派该vc来覆盖全屏幕了。图8-3展示了一个当前的上下文的演示,它只覆盖了spilt vc的一个子视图控制器。

Figure 8-3 The current context presentation style                                   --当前上下文表示样式

Note

When presenting a view controller using the UIModalPresentationFullScreen style, UIKit normally removes the views of the underlying view controller after the transition animations finish. You can prevent the removal of those views by specifying the UIModalPresentationOverCurrentContext style instead. You might use that style when the presented view controller has transparent areas that let underlying content show through.

--当使用UIModalPresentationFullScreen样式来呈现vc时,UIKit通常会在转换动画完成后移除底层vc的视图。此时 ,作为顶替, 您可以通过使用UIModalPresentationOverFullScreen样式来防止删除这些视图。当呈现的vc具有“为了显示底层内容的”透明区域时,也可以使用该样式。

The view controller that defines the presentation context can also define the transition animations to use during the presentation. Normally, UIKit animates view controllers onscreen using the value in the modalTransitionStyle property of the presented view controller. If the presentation context view controller has its providesPresentationContextTransitionStyle set to YES, UIKit uses the value in that view controller’s modalTransitionStyle property instead.

--定义了“显示上下文的” vc还可以定义表示期间要使用的过渡动画。通常,UIKit使用被显示vc中的modalTransitionStyle属性值来动画屏幕上的vc。如果“上下文显示” 的vc 的providesPresentationContextTransitionStyle设置为YES,则UIKit将使用该vc的modAltTransitionStyle属性值, 而非modalTransitionStyle属性的值。

When transitioning to a horizontally compact environment, the current context styles adapt to the UIModalPresentationFullScreen style. To change that behavior, use an adaptive presentation delegate to specify a different presentation style or view controller.

--当转换到水平压缩环境时,当前上下文样式将去适应UIModalPresentationFullScreen样式。若要更改该行为,请使用自适应表示的委托 来指定其他表示样式或vc。

Custom Presentation Styles                                                      --自定义显示样式

The UIModalPresentationCustom style lets you present a view controller using a custom style that you define. Creating a custom style involves subclassing UIPresentationController and using its methods to animate any custom view onto the screen and to set the size and position of the presented view controller. The presentation controller also handles any adaptations that occur because of changes to the presented view controller’s traits.

--UIModalPresentationCustom样式允许您使用自定义样式来呈现vc。创建自定义样式需要子类化UIPresentationController,并使用其方法在屏幕上设置任何自定义视图的动画,以及设置被显示vc的大小和位置。显示vc还处理 那些“由于被显示的vc特性的发生更改”而需要作出的调整。

For information on how to define a custom presentation controller, see Creating Custom Presentations.

--有关如何定义自定义演示文稿vc的信息,请参见超链接: 创建自定义演示文稿。

Transition Styles                                                                                            --过渡样式

Transition styles determine the type of animations used to display a presented view controller. For the built-in transition styles, you assign one of the standard transition styles to the modalTransitionStyle property of the view controller you want to present. When you present the view controller, UIKit creates the animations that correspond to that style. For example, Figure 8-4 illustrates how the standard slide-up transition (UIModalTransitionStyleCoverVertical) animates the view controller onscreen. View controller B starts offscreen and animates up and over the top of view controller A. When view controller B is dismissed, the animation reverses so that B slides down to reveal A.

--过渡样式决定用于显示被表示vc的动画风格。对于内置的过渡样式,你可以将一个“标准的过渡样式”赋值给“你要显示的vc”的modAltTransitionStyle属性。你显示vc时,UIKit将创建与该样式对应的动画。例如,图8-4说明了“标准的向上滑动的过渡样式”(uimodAltTransitionStyleCoverVertical)是如何在屏幕上动画vc的。vc B从屏幕外开始,向上动画到vc A的顶部直至完全覆盖。当vc B 被解除时,动画反转,使B向下滑动以显示A。

Figure 8-4A transition animation for a view controller                       --vc的过渡动画

You can create custom transitions using an animator object and transitioning delegate. The animator object creates the transition animations for placing the view controller onscreen. The transitioning delegate supplies that animator object to UIKit at the appropriate time. For information about how to implement custom transitions, see Customizing the Transition Animations.

--你可以使用 animator对象 和 过渡的委托 来创建自定义“过渡”。animator对象创建用于将vc放置在屏幕上的过渡动画。“过渡委托”会在恰当的时机将 该animator对象 提供给UIKit。有关如何实现自定义转换的信息,参考超链接: 自定义过渡动画。

Presenting Versus Showing a View Controller                    --”显示“与“展示”一个vc的异同

The UIViewController class offers two ways to display a view controller:

--UIViewController类提供了两种方式来显示视图控制器

  • The showViewController:sender: and showDetailViewController:sender: methods offer the most adaptive and flexible way to display view controllers. These methods let the presenting view controller decide how best to handle the presentation. For example, a container view controller might incorporate the view controller as a child instead of presenting it modally. The default behavior presents the view controller modally.
             --showViewController:sender:showDetailViewController:sender:方法提供了用于显示vc的最自适应和最灵活的方式。这些方法让在显示的vc决定如何最好地处理呈现。例如,容器vc可以将vc合并为子控件,而不是以模式表示它。默认行为是模态显示vc的。

  • The presentViewController:animated:completion: method always displays the view controller modally. The view controller that calls this method might not ultimately handle the presentation but the presentation is always modal. This method adapts the presentation style for horizontally compact environments.
             --presentViewController:animated:completion:方法始终以模态显示vc。调用此方法的vc最终可能不处理显示,但显示始终都是模态的。此方法遵循水平紧凑环境的表示样式。

The showViewController:sender: and showDetailViewController:sender: methods are the preferred way to initiate presentations. A view controller can call them without knowing anything about the rest of the view controller hierarchy or the current view controller’s position in that hierarchy. These methods also make it easier to reuse view controllers in different parts of your app without writing conditional code paths.

--showViewController:sender:showDetailViewController:sender:方法是启动文稿演示的偏好方法。vc可以调用它们,而不必知道vc层次结构的其余部分或当前vc在该层次结构中的位置。这些方法使得在app的不同部分重用vc更容易,而无需编写条件代码路径。

Presenting a View Controller                                                                       --显示一个vc

There are several ways to initiate the presentation of a view controller:

--以下有几种方法可以初始化vc的表示:

  • Use a segue to present the view controller automatically. The segue instantiates and presents the view controller using the information you specified in Interface Builder. For more information on how to configure segues, see Using Segues.
             --使用segue可以自动表示vc。segue使用Interface Builder中的信息来实例化并显示vc。有关如何配置segue的更多信息,请参考超链接: 使用segue。

  • Use the showViewController:sender: or showDetailViewController:sender: method to display the view controller. In custom view controllers, you can change the behavior of these methods to something more suitable for your view controller.
             --使用showViewController:sender:showDetailViewController:sender:方法显示vc。你可以在你自定义的vc中,将这些方法的行为复写为更符合你要求的vc行为。

  • Call the presentViewController:animated:completion: method to present the view controller modally.
             --调用presentViewController:animated:completion:方法模态地显示vc。

For information about how to dismiss a view controller that you presented using one of these techniques, see Dismissing a Presented View Controller.

--有关如何解除“使用上面的技术展示的”vc的文章,请参考超链接: 关闭显示的vc。

Showing View Controllers                                                                                   --展示vc

When using the showViewController:sender: and showDetailViewController:sender: methods, the process for getting a new view controller onscreen is straightforward:

--如果是使用showViewController:sender:showDetailViewController:sender:方法时,那么在屏幕上获取新vc的过程很简单:

  1. Create the view controller object you want to present. When creating the view controller, it is your responsibility to initialize it with whatever data it needs to perform its task.
            --创建你想显示的vc对象。创建vc时,你要负责初始化vc和vc执行相关执行任务时所需要的数据。

  2. Set the modalPresentationStyle property of the new view controller to the preferred presentation style. This style might not be used in the final presentation.
             --将新vc的modalPresentationStyle属性设置为偏好演示样式。虽然此样式最终可能不被使用。

  3. Set the modalTransitionStyle property of the view controller to the desired transition animation style. This style might not be used in the final animations.
              --将vc的modAltTransitionStyle属性设置为所需的过渡动画样式。虽然此样式最终可能不被使用。

  4. Call the showViewController:sender: and showDetailViewController:sender: method of the current view controller.
              --调用当前vc的showViewController:sender:showDetailViewController:sender:方法

UIKit forwards calls to the showViewController:sender: and showDetailViewController:sender: methods to the appropriate presenting view controller. That view controller can then decide how best to perform the presentation and can change the presentation and transition styles as needed. For example, a navigation controller might push the view controller onto its navigation stack.

--UIKit将对showViewController:sender:showDetailViewController:sender:方法的调用, 转发给恰当的在显示的vc。然后,vc可以决定怎样执行表示、并可以根据需要更改显示和转换样式。例如,导航vc可以将vc压到其导航堆栈上。

For information about the differences between showing view controllers and presenting them modally, see Presenting Versus Showing a View Controller.

--有关显示vc和模态显示vc之间的差异的信息,参考超链接: 展示与显示vc的异同。

Presenting View Controllers Modally                                           --模态显示vc

When presenting a view controller directly, you tell UIKit how you want the new view controller to be displayed and how it should be animated onscreen.

--当直接显示vc时,您可以告诉UIKit您希望如何显示新的vc以及如何在屏幕上设置动画。

  1. Create the view controller object you want to present.

    When creating the view controller, it is your responsibility to initialize it with whatever data it needs to perform its task.
    --创建你想要显示的vc对象。
    --创建vc时,你要负责初始化vc和vc执行相关执行任务时所需要的数据。

  2. Set the modalPresentationStyle property of the new view controller to the desired presentation style.
      --将新vc的modalPresentationStyle属性设置为你所需的表示样式。

  3. Set the modalTransitionStyle property of the view controller to the desired animation style.
     --将vc的modAltTransitionStyle属性设置为所需的动画样式。

  4. Call the presentViewController:animated:completion: method of the current view controller.
    --调用当前vc的presentViewController:animated:completion:方法。

The view controller that calls the presentViewController:animated:completion: method may not be the one that actually performs the modal presentation. The presentation style determines how that view controller is to be presented, including the characteristics required of the presenting view controller. For example, a full-screen presentation must be initiated by a full-screen view controller. If the current presenting view controller is not suitable, UIKit walks the view controller hierarchy until it finds one that is. Upon completion of a modal presentation, UIKit updates the presentingViewController and presentedViewController properties of the affected view controllers.

--调用presentViewController:animated:completion:方法的vc有可能不是实际执行模态表示的vc。演示样式确定如何显示vc,包括vc显示时要求的特性。例如,全屏演示必须由全屏vc启动。如果当前的显示vc不是全屏,UIKit将遍历vc层次结构,直至找到为止。模态演示完成后,UIKit将更新受到影响的vc的presentingViewControllerpresentedViewController属性

Listing 8-1 demonstrates how to present a view controller programmatically. When the user adds a new recipe, the app prompts the user for basic information about the recipe by presenting a navigation controller. A navigation controller was chosen so that there would be a standard place to put a Cancel and Done button. Using a navigation controller also makes it easier to expand the new recipe interface in the future. All you have to do is push new view controllers on the navigation stack.

--清单8-1的代码 展示了如何以编程方式呈现vc。当用户添加新元素时,app会通过”在显示的导航vc“来提示用户有关元素的基本信息。由于选择了一个导航vc,所以会有一个标准的地方来放置一个“取消并完成”按钮。使用导航vc还可以更容易地在以后 扩展新元素的界面。你所要做的就是在导航堆栈上压栈新的vc。

Listing 8-1 Presenting a view controller programmatically                                                   --编程方式显示一个vc

  1. - (void)add:(id)sender {
  2. // Create the root view controller for the navigation controller
  3. // The new view controller configures a Cancel and Done button for the
  4. // navigation bar.
  5. RecipeAddViewController *addController = [[RecipeAddViewController alloc] init];
  6. addController.modalPresentationStyle = UIModalPresentationFullScreen;
  7. addController.transitionStyle = UIModalTransitionStyleCoverVertical;
  8. [self presentViewController:addController animated:YES completion: nil];
  9. }

Presenting a View Controller in a Popover                                    --在弹窗中显示vc

Popovers require additional configuration before you can present them. After setting the modal presentation style to UIModalPresentationPopover, configure the following popover-related attributes:

--弹窗需要额外的配置才能被显示。将模态演示样式设置为UIModalPresentationPopover后,请配置以下与popover相关的属性:

  • Set the preferredContentSize property of your view controller to the desired size.
          --将vc的preferredContentSize属性设置为所需的大小

  • Set the popover anchor point using the associated UIPopoverPresentationController object, which is accessible from the view controller’s popoverPresentationController property. Set only one of the following:
       --使用关联的UIPopoverPresentationController对象设置popover的定位点,该对象可从视图控制器的popoverPresentationController属性访问。以下选项仅设置其一:

    • Set the barButtonItem property to a bar button item.
      --将barbuttonem属性设置为条栏的按钮项。

    • Set the sourceView and sourceRect properties to a specific region in one of your views.
      sourceViewsourceRect属性设置为某个视图中的特定区域。

You can use the UIPopoverPresentationController object to make other adjustments to the popover’s appearance as needed. The popover presentation controller also supports a delegate object that you can use to respond to changes during the presentation process. For example, you can use the delegate to respond when the popover appears, disappears, or is repositioned on the screen. Fore more information about this object, see UIPopoverPresentationController Class Reference.

--您可以使用uipopompoverpresentationcontroller对象根据需要 对popover的外观进行其他的调整。popover演示控制器还支持一个委托对象,您可以使用该对象来响应演示过程中的更改。例如,当弹出窗口出现、消失或在屏幕上重新定位时,都可以使用委托来进行响应。有关此对象的详细信息,请参考超链接: “uipopoppoverpresentationcontroller类引用”。

Dismissing a Presented View Controller                         --解散一个被呈现的视图控制器

To dismiss a presented view controller, call the dismissViewControllerAnimated:completion: method of the presenting view controller. You can also call this method on the presented view controller itself. When you call the method on the presented view controller, UIKit automatically forwards the request to the presenting view controller.

--要取消一个被呈现的vc,就调用正在呈现vc的viewcontrolleranimated:completion: 方法。你也可以在被呈现的vc本身上调用这个方法。当你在被呈现的vc自身上调用这个方法时,UIKit会自动将请求转发给正在作呈现的vc。

Always save any important information from a view controller before dismissing it. Dismissing a view controller removes it from the view controller hierarchy and removes its view from the screen. If you do not have a strong reference to the view controller stored elsewhere, dismissing it releases the memory associated with it.

--在解除vc之前,请始终保存该vc中的任何重要信息。解除vc会将其从vc层次结构中移除,并将其视图从屏幕中移除。如果在其他位置没有对该vc的强引用,则解除该vc会释放与其关联的内存。

If the presented view controller must return data to the presenting view controller, use the delegation design pattern to facilitate the transfer. Delegation makes it easier to reuse view controllers in different parts of your app. With delegation, the presented view controller stores a reference to a delegate object that implements methods from a formal protocol. As it gathers results, the presented view controller calls those methods on its delegate. In a typical implementation, the presenting view controller makes itself the delegate of its presented view controller.

--如果被呈现的vc必须将数据返回给正在呈现的vc,请使用委托设计模式来促进数据的传输。委派设计模式使得在app的不同部分中重用vc变得更加容易。通过委托,被呈现的vc存储委托对象的引用,该委托对象实现来自正式协议的方法。当它收集结果时,被呈现的vc通过其委托调用这些方法。在典型的实现中,正在呈现的vc让自己成为其被呈现vc的委托。

Presenting a View Controller Defined in a Different Storyboard        --呈现在不同storyboard中定义的vc

Although you can create segues between view controllers in the same storyboard, you cannot create segues between storyboards. When you want to display a view controller stored in a different storyboard, you must instantiate that view controller explicitly before presenting it, as shown in Listing 8-2. The example presents the view controller modally but you could push it onto a navigation controller or display it in other ways.

--尽管您可以在同一个storyboard中创建vc之间的segue,但是您不能在storyboard之间创建segue。当您想要显示存储在不同storyboard中的vc时,您必须在显示它之前显式地实例化该vc,如清单8-2 的代码所示。这个例子模态展示了vc,但是你可以将它推到一个导航vc上或者以其他方式显示它。

Listing 8-2 Loading a view controller from a storyboard     --从故事板中加载视图控制器

  1. UIStoryboard* sb = [UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil];
  2. MyViewController* myVC = [sb instantiateViewControllerWithIdentifier:@"MyViewController"];
  3. // Configure the view controller.
  4. // Display the view controller
  5. [self presentViewController:myVC animated:YES completion:nil];

There is no requirement that you create multiple storyboards in your app. Here, though, are a few cases where multiple storyboards might be useful:

--你不需要在你的app中创建多个故事板。但是,这里有几个关于多个故事板的例子:

  • You have a large programming team, with different portions of the user interface assigned to different parts of the team. Each team owns its portion of the user interface in a different storyboard file to minimize contention.
           --您有一个大型的编程团队,需要将用户界面的不同部分分配给团队的不同部分。每个团队在不同的storyboard文件中都拥有自己的用户界面部分,以最小化争用。

  • You purchased or created a library that predefines a collection of view controller types; the contents of those view controllers are defined in a storyboard provided by the library.
            --您购买或创建了一个“预先定义vc类型集合”的库; 那些vc的内容是在“库提供的storyboard“中定义的。

  • You have content that needs to be displayed on an external screen. In this case, you might keep all of the view controllers associated with the alternate screen inside a separate storyboard. An alternative pattern for the same scenario is to write a custom segue.
          --您有需要在外部屏幕上显示的内容。在这种情况下,您可以将所有“与备用屏幕相关的”vc都保存在一个单独的storyboard中。相同场景的另一种模式是编写自定义segue。

(2)Using Segues

Use segues to define the flow of your app’s interface. A segue defines a transition between two view controllers in your app’s storyboard file. The starting point of a segue is the button, table row, or gesture recognizer that initiates the segue. The end point of a segue is the view controller you want to display. A segue always presents a new view controller, but you can also use an unwind segue to dismiss a view controller.

Figure 9-1A segue between two view controllers

You do not need to trigger segues programmatically. At runtime, UIKit loads the segues associated with a view controller and connects them to the corresponding elements. When the user interacts with the element, UIKit loads the appropriate view controller, notifies your app that the segue is about to occur, and executes the transition. You can use the notifications sent by UIKit to pass data to the new view controller or prevent the segue from happening altogether.

Creating a Segue Between View Controllers

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer. You can also create segues from cell-based views such as tables and collection views. Figure 9-2 shows the creation of a segue that displays a new view controller when a table row is tapped.

Figure 9-2Creating the segue relationship

Note

Some elements support multiple segues. For example, a table row lets you configure different segues for taps in the row’s accessory button and taps in the rest of the row.

When you release the mouse button, Interface Builder prompts you to select the type of relationship you want to create between the two view controllers, as shown in Figure 9-3. Select the segue that corresponds to the transition you want.

Figure 9-3Selecting the type of segue to create

When selecting the relationship type for your segue, select an adaptive segue whenever possible. Adaptive segues adjust their behavior automatically based on the current environment. For example, the behavior of a Show segue changes based on the presenting view controller. Nonadaptive segues are provided for apps that must also run on iOS 7, which does not support adaptive segues. Figure 9-1 lists the adaptive segues and how they behave in your app.

Table 9-1Adaptive segue types

Segue type

Behavior

Show (Push)

This segue displays the new content using the showViewController:sender: method of the target view controller. For most view controllers, this segue presents the new content modally over the source view controller. Some view controllers specifically override the method and use it to implement different behaviors. For example, a navigation controller pushes the new view controller onto its navigation stack.

UIKit uses the targetViewControllerForAction:sender: method to locate the source view controller.

Show Detail (Replace)

This segue displays the new content using the showDetailViewController:sender: method of the target view controller. This segue is relevant only for view controllers embedded inside a UISplitViewController object. With this segue, a split view controller replaces its second child view controller (the detail controller) with the new content. Most other view controllers present the new content modally.

UIKit uses the targetViewControllerForAction:sender: method to locate the source view controller.

Present Modally

This segue displays the view controller modally using the specified presentation and transition styles. The view controller that defines the appropriate presentation context handles the actual presentation.

Present as Popover

In a horizontally regular environment, the view controller appears in a popover. In a horizontally compact environment, the view controller is displayed using a full-screen modal presentation.

After creating a segue, select the segue object and assign an identifier to it using the attributes inspector. During a segue, you can use the identifier to determine which segue was triggered, which is especially useful if your view controller supports multiple segues. The identifier is included in the UIStoryboardSegue object delivered to your view controller when the segue is performed.

Modifying a Segue’s Behavior at Runtime

Figure 9-4 shows what happens when a segue is triggered. Most of the work happens in the presenting view controller, which manages the transition to the new view controller. The configuration of the new view controller follows essentially the same process as when you create the view controller yourself and present it. Because segues are configured from storyboards, both view controllers involved in the segue must be in the same storyboard.

Figure 9-4Displaying a view controller using a segue

During a segue, UIKit calls methods of the current view controller to give you opportunities to affect the outcome of the segue.

  • The shouldPerformSegueWithIdentifier:sender: method gives you an opportunity to prevent a segue from happening. Returning NO from this method causes the segue to fail quietly but does not prevent other actions from happening. For example, a tap in a table row still causes the table to call any relevant delegate methods.

  • The prepareForSegue:sender: method of the source view controller lets you pass data from the source view controller to the destination view controller. The UIStoryboardSegue object passed to the method contains a reference to the destination view controller along with other segue-related information.

Creating an Unwind Segue

Unwind segues let you dismiss view controllers that have been presented. You create unwind segues in Interface Builder by linking a button or other suitable object to the Exit object of the current view controller. When the user taps the button or interacts with the appropriate object, UIKit searches the view controller hierarchy for an object capable of handling the unwind segue. It then dismisses the current view controller and any intermediate view controllers to reveal the target of the unwind segue.

To create an unwind segue

  1. Choose the view controller that should appear onscreen at the end of an unwind segue.

  2. Define an unwind action method on the view controller you chose.

    The Swift syntax for this method is as follows:

    • @IBAction func myUnwindAction(unwindSegue: UIStoryboardSegue)

    The Objective-C syntax for this method is as follows:

    • - (IBAction)myUnwindAction:(UIStoryboardSegue*)unwindSegue
  3. Navigate to the view controller that initiates the unwind action.

  4. Control-click the button (or other object) that should initiate the unwind segue. This element should be in the view controller you want to dismiss.

  5. Drag to the Exit object at the top of the view controller scene.

  6. Select your unwind action method from the relationship panel.

You must define an unwind action method in one of your view controllers before trying to create the corresponding unwind segue in Interface Builder. The presence of that method is required and tells Interface Builder that there is a valid target for the unwind segue.

Use the implementation of your unwind action method to perform any tasks that are specific to your app. You do not need to dismiss any view controllers involved in the segue yourself; UIKit does that for you. Instead, use the segue object to fetch the view controller being dismissed so that you can retrieve data from it. You can also use the unwind action to update the current view controller before the unwind segue finishes.

Initiating a Segue Programmatically

Segues are usually triggered because of the connections you create in your storyboard file. However, there may be times when you cannot create segues in your storyboard, perhaps because the destination view controller is not yet known. For example, a game app might transition to different screens depending on the outcome of the game. In those situations, you can trigger segues programmatically from your code using the performSegueWithIdentifier:sender: method of the current view controller.

Listing 9-1 illustrates a segue that presents a specific view controller when rotating from portrait to landscape. Because the notification object in this case provides no useful information for performing the segue command, the view controller designates itself as the sender of the segue.

Listing 9-1Triggering a segue programmatically

  1. - (void)orientationChanged:(NSNotification *)notification {
  2. UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
  3. if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
  4. !isShowingLandscapeView) {
  5. [self performSegueWithIdentifier:@"DisplayAlternateView" sender:self];
  6. isShowingLandscapeView = YES;
  7. }
  8. // Remainder of example omitted.
  9. }

Creating a Custom Segue

Interface Builder provides segues for all of the standard ways to transition from one view controller to another—from presenting a view controller to displaying a controller in a popover. However, if one of those segues doesn’t do what you want, you can create a custom segue.

The Life Cycle of a Segue

To understand how custom segues work, you need to understand the life cycle of a segue object. Segue objects are instances of the UIStoryboardSegue class or one of its subclasses. Your app never creates segue objects directly; UIKit creates them when a segue is triggered. Here’s what happens:

  1. The view controller to be presented is created and initialized.

  2. The segue object is created and its initWithIdentifier:source:destination: method is called. The identifier is the unique string you provided for the segue in Interface Builder, and the two other parameters represent the two controller objects in the transition.

  3. The presenting view controller’s prepareForSegue:sender: method is called. See Modifying a Segue’s Behavior at Runtime.

  4. The segue object’s perform method is called. This method performs a transition to bring the new view controller onscreen.

  5. The reference to the segue object is released.

Implementing a Custom Segue

To implement a custom segue, subclass UIStoryboardSegue and implement the following methods:

  • Override the initWithIdentifier:source:destination: method and use it to initialize your custom segue object. Always call super first.

  • Implement the perform method and use it to configure your transition animations.

Note

If your implementation adds properties to configure the segue, you cannot configure these attributes in Interface Builder. Instead, configure the custom segue’s additional properties in the prepareForSegue:sender: method of the source view controller that triggered the segue.

Listing 9-2 shows a very simple custom segue. This example simply presents the destination view controller without any sort of animation, but you can extend this idea with your own animations as necessary.

Listing 9-2A custom segue

  1. - (void)perform {
  2. // Add your own animation code here.
  3. [[self sourceViewController] presentViewController:[self destinationViewController] animated:NO completion:nil];
  4. }

0306--iOS之阅读View Controller Programming Guide for iOS---(三)Presentations and Trasitions相关推荐

  1. View Controller Programming Guide for iOS---(七)---Resizing the View Controller’s Views

    Resizing the View Controller's Views A view controller owns its own view and manages the view's cont ...

  2. View Programming Guide for iOS官方文档翻译一

    版权声明:本文为博主原创文章,转载请声明出处,谢谢! 本文档的官方英文原版地址 关于窗口和视图 在iOS中,您可以使用窗口(windows)和视图(views)在屏幕上显示APP的内容. 窗口(Win ...

  3. View Programming Guide for IOS:Views笔记

    1.执行画图代码: "当前绘图上下文"只在执行视图的drawRect方法期间才有效.UIKit有可能会在后续调用drawRect方法时声称一个新的视图上下文,所以你不能试图去缓存这 ...

  4. 打造轻量化的View Controller

    2019独角兽企业重金招聘Python工程师标准>>> 本文由破船译自objc 小引 很早以前就看到了这篇文章,该文是Lighter View Controllers中比较重要的一篇 ...

  5. 官方文档链接(Core Graphics、View Controller、UICollectionView、UITableView、第三方库)

    Core Graphics Quartz 2D Programming Guide Core Graphics (Framework) Drawing(UIKit).Images and PDF(UI ...

  6. iOS VoiceOver Programming Guide

    前言 VoiceOver是苹果"读屏"技术的名称,属于辅助功能的一部分.VoiceOver可以读出屏幕上的信息,以帮助盲人进行人机交互. 这项技术在苹果的各个系统中都可以看到,OS ...

  7. [iOS]Advanced Memory Management Programming Guide 高级内存管理编程指南(官方文档翻译)

    Advanced Memory Management Programming Guide - 高级内存管理编程指南(官方文档翻译) 版权声明:本文为博主原创翻译,如需转载请注明出处. 新博客文章地址: ...

  8. IOS:Warning: Attempt to dismiss from view controller xxxxxxxxxController: 0x15bd67f0 while a prese

    Warning: Attempt to dismiss from view controller <SetUpViewController: 0x15bd67f0> while a pre ...

  9. IOS:Warning: Attempt to dismiss from view controller xxxxxxxxxController: 0x13f6840e0

    Ios:在你使用 presentModalViewController加载过多的视图过程中,退出当前视图self dismissViewControllerAnimated:YES completio ...

最新文章

  1. 【H2645】H.264的宏块和H.265的编码树单元总结
  2. 科研费4年翻3倍,全球科研队伍突破8000人,滴滴致力打造出行领域核心技术
  3. 第九次作业——测试报告与用户手册
  4. mysql5.7安装配置
  5. java打开文件出错_打开文件错误太多,java.io.FileNotFoundException
  6. MutationObserver详解
  7. 分布式资源调度—YARN框架
  8. 早停!? earlystopping for keras
  9. JavaFX中的塔防
  10. aes-128算法加密_加密算法问题-人工智能中的一种约束满意问题
  11. 科来无线抓包基础知识扫盲
  12. 使用API网关构建微服务
  13. 恢复计算机文件的软件,删除文件恢复大师软件
  14. 国仁猫哥:视频号企业运营指南教科书;不可错失的红利。
  15. Spring源码分析总结(二)-Spring AOP 解析aop:aspectj-autoproxy
  16. LCM液晶显示模组是什么,液晶显示模块有几种类型呢
  17. 炒股程序化交易接口怎么获取实时行情
  18. PC 视频播放器(超好)
  19. Failed to open \EFI\BOOT\mmx64.efi问题解决
  20. 金蝶ERP供应链之采购管理

热门文章

  1. android openCV检测图像的基本特征,包括Canny边缘检测、Harris角点检测、霍夫直线检测-基于Android studio
  2. 主机和虚拟机之间不能互通解决办法
  3. Direct3D中的纹理映射
  4. 一篇优秀的硕士毕业论文是怎么写成的
  5. 如果用一个循环数组q[0..m-1]表示队列时,该队列只有一个队列头指针front,不设队列尾指针rear,求这个队列中从队列投到队列尾的元素个数(包含队列头、队列尾)。
  6. 游戏AI行为选择算法一览
  7. RISC-V指令集架构------RV32F/RV32D浮点扩展指令集
  8. QT 当窗口过小时发送错误 setGeometry: Unable to set geometry 25*160...Resulting geometry:116*160...
  9. CAD怎么打印彩色图纸
  10. P4语言的特性、P4语言和P4交换机的工作原理和流程简介