简体   繁体   English

iPhone在所有视图上的持久按钮

[英]iphone persistent button on all views

I have a navigation app that has many screens the user navigates to. 我有一个导航应用程序,它具有用户浏览到的许多屏幕。 A handful of views manages these screens dynamically. 少数视图可动态管理这些屏幕。 What I want to try to do is add a button that will always show up on every screen the user views. 我想尝试做的是添加一个按钮,该按钮将始终显示在用户查看的每个屏幕上。 I need to do this so that the user is always able to preform the action associated with the button regardless of where they are in the app. 我需要这样做,以便用户始终能够执行与按钮关联的操作,而不管他们在应用程序中的位置如何。

Is it possible to achieve this by adding this button only once and having it passed between views like my navigation bar is? 是否可以通过仅添加一次此按钮并将其在视图之间(如导航栏一样)传递来实现此目的? Or do I just have to man up and add this button and its functionality to every single view file I have? 还是我只需要手动将该按钮及其功能添加到我拥有的每个单个视图文件中?

Thanks 谢谢

I would say it probably depends on what the button does. 我会说这可能取决于按钮的功能。 If the button is generic to all views, meaning it affects all views the same exact way so no customization for a given view is needed, then a way to do this would be to include the function in the App Delegate or as a subclass to your Navigation controller. 如果按钮对所有视图都是通用的,这意味着它以相同的方式影响所有视图,因此无需为给定视图进行自定义,那么一种实现方法是将函数包含在App Delegate中或作为您的子类。导航控制器。

You can then use the rightBarButtonItem to always show the same button and just access that method. 然后,您可以使用rightBarButtonItem始终显示相同的按钮,并仅访问该方法。 You would just have to add code for the rightBarButtonItem in each viewDidLoad (but they'd all be the same). 您只需要在每个viewDidLoadrightBarButtonItem添加代码即可(但是它们都相同)。

I did something similar to this with an "Upgrade" button on one project. 我在一个项目上使用“升级”按钮做了类似的事情。 Since all the button does is launch the AppStore to the paid version, it's independent of all views and I can place it anywhere. 由于所有按钮的作用都是将AppStore启动到付费版本,因此它独立于所有视图,因此我可以将其放置在任何地方。

You can put the button on the navigation bar if you want. 您可以根据需要将按钮放在导航栏上。 Alternately, the more generic way to do this would be to split your single view into two views. 或者,更通用的方法是将单个视图分为两个视图。 One is small and only contains your button but always stays on the screen. 一个很小,只包含您的按钮,但始终停留在屏幕上。 The second is your workspace and you swap in and out the views that are displaying the current content. 第二个是您的工作区,您可以换入和换出显示当前内容的视图。 You'll note that this is the way the navigation controls and tab-bar controls work. 您会注意到,这是导航控件和标签栏控件的工作方式。 The last way to do this would be to put different buttons, in the same place, on each view and have them all trigger the same action. 最后一种方法是将不同的按钮放在每个视图的相同位置,并使它们都触发相同的操作。 As far as the user is concerned this looks like the same button. 就用户而言,这看起来像是同一按钮。 Disadvantage here is that you can't alter the button across all views in a simple manner. 此处的缺点是您不能以简单的方式在所有视图中更改按钮。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM