简体   繁体   English

如何优雅地从一个嵌入式视图导航到另一个视图(在IB中使用布局)?

[英]How can I elegantly navigate from one embedded view to another (with layout in IB)?

In this app the user can do either Activity A or B. When doing A, the viewer sees embedded View A; 用户可以在该应用程序中执行“活动A”或“活动B”。 but also during A the viewer can choose to see embedded View B. (The same goes for Activity B, only with converse views.) 而且在A期间,查看者可以选择查看嵌入的ViewB。(Activity B也是如此,只适用于相反的视图。)

My setup in IB is diagrammed below. 我在IB中的设置如下图所示。 Not knowing any better way, I created twice as many activity scenes as I would have liked: For the "Do Activity A" branch, I have two side-by-side copies of the Activity A scene. 不知道有什么更好的方法,我创建了两倍于我想要的活动场景:对于“执行活动A”分支,我有两个并排的活动A场景副本。 The first one has an embed segue to View A; 第一个嵌入到视图A的序列中; and the second, an embed segue to View B. (The "B" setup is the converse.) 第二个是嵌入到视图B的序列。(“ B”设置是相反的。)

To avoid more duplication, I did make both activity branches share Views A and B. And although the activity scenes were doubled, the classes that they link to (ActivityAViewController and ActivityBViewController) were not. 为避免重复,我确实使两个活动分支都共享视图A和B。尽管活动场景增加了一倍,但它们链接到的类(ActivityAViewController和ActivityBViewController)却没有。 I haven't yet noticed any complications from that. 我还没有注意到任何复杂的情况。 . . .

In contrast to other workarounds I've seen, I chose the above "push–pop" layout because Views A and B are expensive. 与我所看到的其他解决方法相比,我选择了上面的“推送”布局,因为视图A和B都很昂贵。 I don't want to discard the primary view just to load the secondary, and I don't want to load both unnecessarily. 我不想仅仅为了加载次要视图而放弃主要视图,也不想不必要地加载两个视图。 Also, I want to do this in IB because the real app is more complicated, and seeing all of my layout there helps me understand and develop it. 另外,我想在IB中执行此操作,因为真正的应用程序更复杂,并且看到那里的所有布局都有助于我理解和开发它。 With that said, my approach still seems klutzy. 话虽如此,我的方法仍然显得愚蠢。

My questions are 我的问题是

  1. Is there a better way to set this up in IB? 是否有更好的方法在IB中进行设置?
  2. What happens with the activity scenes' shared view controllers? 活动场景的共享视图控制器会发生什么? Is a separate class instance created for each, or do the two scenes end up sharing the same instance? 是为每个实例创建一个单独的类实例,还是两个场景最终共享同一实例?
  3. Similarly, what happens with the shared views? 同样,共享视图会怎样?

Apple's explanations seem clear enough, though they assume more knowledge of Cocoa Touch and related jargon than I currently have. 苹果的解释似乎很清楚,尽管他们认为我比现在更了解Cocoa Touch和相关术语。 Any clarifications and insights will be appreciated. 任何澄清和见解将不胜感激。

具有嵌入式视图的导航情节提要

Is there a better way to set this up in IB? 是否有更好的方法在IB中进行设置?

There are other ways to set it up. 还有其他设置方法。 They all have tradeoffs. 他们都有权衡。 Your method has a certain look, and if you want to hook it up differently, it will look a little different. 您的方法具有一定的外观,如果您想以不同的方式进行连接,则外观会有所不同。 Your method also has the advantage that all of the navigation is handled by the Storyboard; 您的方法还具有以下优点:所有导航都由情节提要进行处理; no extra code is needed. 不需要额外的代码。

Alternatives include imbedding views A and B in their own little navigation controller. 备选方案包括将视图A和B嵌入其自己的小导航控制器中。 Then switching to view the other would involve clicking on that navigationController. 然后切换到另一个视图将涉及单击该navigationController。 This avoids the duplication of the outer Activity A and B controllers, but it would change the look by adding a navigationBar to the embedded views. 这避免了外部Activity A和B控制器的重复,但是它将通过向嵌入式视图添加navigationBar来更改外观。

You could also do that and hide the navigation bar and pass a message from the outer Activity A or B controller to tell it when to push the other viewController. 您也可以这样做,并隐藏导航栏,并从外部Activity A或B控制器传递一条消息,以告知何时按下另一个viewController。 This has the disadvantage of some additional nontrivial coding; 这具有一些额外的非平凡编码的缺点。 the Storyboard no longer manages the navigation for you. 情节提要不再为您管理导航。

You could also run this in a UITabBarController. 您也可以在UITabBarController中运行它。 That has the disadvantage of creating both Activity A and Activity B at the same time if you only need one, but you can freely switch between them without any recreation of any of the views. 如果只需要一个,则具有同时创建活动A和活动B的缺点,但是您可以在它们之间自由切换而无需重新创建任何视图。

What happens with the activity scenes' shared view controllers? 活动场景的共享视图控制器会发生什么? Is a separate class instance created for each, or do the two scenes end up sharing the same instance? 是为每个实例创建一个单独的类实例,还是两个场景最终共享同一实例?

Every segue (including embedded segues) creates a new viewController instance. 每个序列(包括嵌入式序列)都会创建一个新的viewController实例。 No instances are shared. 没有实例共享。

Similarly, what happens with the shared views? 同样,共享视图会怎样?

A new instance of A and B will be created each time. 每次都会创建一个新的A和B实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用情节提要或IB的情况下从嵌入式集合视图的单元导航到另一个视图控制器? - How to navigate from an embedded collection view’s cell to another view controller without using Storyboard or IB? 如何将IB中的自动布局约束从一个视图复制到另一个视图? - How to copy auto-layout constraints in IB from one view to another? 如何从一个视图控制器导航到另一视图控制器? - How to navigate from one view controller to another view controller? 如何在ios中从一个视图导航到另一个视图? - How to Navigate from one view to another view in ios? iOS无法从表视图控制器导航到另一个 - IOS can't navigate from a table view controller to another one 单击按钮后,如何从视图控制器导航到另一个视图控制器? - How i can navigate from View Controller to another View Controller once clicking on button? 如何从一个视图导航到另一个视图而无需导航? 在iPhone中 - how to navigate from one view to another without navigation? in iphone 如何通过单击按钮在 Swift UI 中从一个视图导航到另一个视图 - How to navigate from one view to another in Swift UI on a click of button 如何在 Swift 中以编程方式从一个控制器导航到另一个视图控制器? - How to navigate from one controller to another View Controller programmatically in Swift? 如何在不使用 NavigationController 的情况下从一个视图导航到另一个视图? - How navigate from one view to another without using NavigationController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM