多个标签中的同一视图 [英] Same view in multiple tabs

查看:52
本文介绍了多个标签中的同一视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个带有四个选项卡的UITabBar ...每个选项卡都有其自己的自定义UIViewController.其中三个在屏幕上的同一位置有一个地图以及其他公用的UI元素.在其中两个视图中,地图最初是隐藏的.

My app has a UITabBar with four tabs... each with its own custom UIViewController. Three of them have a map plus other UI elements in common, in the same place on the screen.. in two of these views the map is initially hidden.

使这三个视图成为同一视图控制器的同一实例,并隐藏/显示元素似乎更有意义.使用标签栏可以做到这一点吗?

It seems like it would make more sense to make these three view the same instance of the same view controller.. and just hide/show elements. Is it possible to do this with the tab bar?

推荐答案

我认为回答是"的人没有足够仔细地阅读您的问题.您询问是否可以使这三个视图与同一视图控制器具有相同的 instance ".您当然可以使用同一UIViewController子类的三个不同实例,但我认为您不想使用同一实例.

I don't think those who answered "yes" read your question closely enough. You asked if it's possible to "make these three view the same instance of the same view controller". You can certainly use three different instances of the same UIViewController subclass, but I don't think you'll want to use the same instance.

老实说,我从来没有尝试过,但是出于以下几个原因,我不希望它能正常工作:

I've honestly never tried this, but I wouldn't logically expect it to work for a couple reasons:

  1. 为每个选项卡显示的标题和图标是通过视图控制器的tabBarItem属性定义的.如果同一UIViewController实例多次出现在选项卡栏控制器的viewControllers数组中,则每个选项卡还将共享相同的tabBarItem,这意味着您将无法为每个选项卡赋予唯一的标签和图标.

  1. The title and icon shown for each tab is defined via the view controller's tabBarItem property. If the same UIViewController instance appeared multiple times in the tab bar controller's viewControllers array, then every tab would also share the same tabBarItem, meaning you'd have no way to give each tab a unique label and icon.

为节省内存,当您从一个选项卡切换到另一个选项卡时,UIKit将卸载已消失的视图控制器的视图.如果您切换到的视图控制器与消失的实例控制器相同,则UIKit可能会在显示视图时尝试卸载其视图.我希望这会产生内存管理错误,可能会导致您的应用程序崩溃并显示EXC_BAD_ACCESS信号.

To conserve memory when you switch from one tab to another, UIKit will unload the view of the view controller that disappeared. If the view controller you've switched to is the same instance as the one that disappeared, UIKit may try to unload its view while it's being displayed. I'd expect this to create memory management bugs that could cause your app to crash with an EXC_BAD_ACCESS signal.

我建议不要使用多个标签中的同一实例,而是推荐以下选项之一:

Rather than using the same instance for multiple tabs, I'd recommend one of these options:

A.使用同一UIViewController子类的多个实例,并设置属性以唯一地配置每个实例.

A. Use the multiple instances of the same UIViewController subclass, and set properties to uniquely configure each instance.

B.创建一个基类UIViewController子类,该子类实现所有三个选项卡所共有的那些方面,然后创建基类的三个子类,这些子类实现每个选项卡所独有的那些方面.

B. Create a base UIViewController subclass that implements those aspects that are common to all three tabs, then create three subclasses of your base class that implement those aspects that are unique to each tab.

这篇关于多个标签中的同一视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆