使按钮在所有视图控制器中持久化 [英] Making a button persistent across all view controllers

查看:18
本文介绍了使按钮在所有视图控制器中持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序的右下角有一个持久按钮.在所有视图转换期间,按钮应保持静态.我无法决定将按钮添加到哪个视图.我知道按钮应该存储在 AppDelegate 中,但我不知道将它添加到窗口之外的其他视图是否有意义.将它添加到窗口的一个缺点是,当有应用程序在后台运行时(即电话),添加的状态栏填充会向下推窗口.一般来说,将它添加到窗口似乎是一个笨拙的解决方案 - 有什么想法吗?

I want to have a persistent button in the bottom right corner of my app. During all view transitions, the button should remain static. I'm having trouble deciding what view to add the button to. I know the button ought to be stored in the AppDelegate, but I don't know what other view it would be sense to add it to except the window. One downside of adding it to the window is that when there's an app running in the background (ie Phone), the added status bar padding will push down the window. In general, adding it to the window seems to be a hacky solution -- any thoughts?

推荐答案

是的,将它添加到 UIWindow 会非常麻烦和挑剔.

Yes, adding it to the UIWindow would be extremely hacky and finicky.

如果您使用 Storyboards 和 iOS 5.0 以上版本,您应该能够使用容器视图并执行如下操作:

If you're using Storyboards and iOS 5.0 onwards, you should be able to use container views and do something like this:

这是另一张图片,显示了第一个视图控制器的结构相当简单:

Here's another picture showing the, rather simplistic, structure of the first View Controller:

左侧的视图控制器有一个容器,然后是一个将按钮放在上面的视图.容器指示导航控制器(直接在右侧)应该出现在其自身内,这种关系由 =([])=> 箭头表示(正式称为 embed segue).最后,导航控制器将其根视图控制器定义为右侧的那个.

The view controller on the left has a container, and then a view which holds the button on top of it. The container indicates that the navigation controller (directly to the right) should appear within itself, that relationship is shown by the =([])=> arrow (formally known as an embed segue). Finally the navigation controller defines its root view controller to the one on the right.

总而言之,第一个视图控制器在容器视图中煎饼,按钮在顶部,所以里面发生的一切都必须有按钮在顶部.

In summary, the first view controller pancakes-in the container view with the button on top, so everything that happens inside has to have the button on top.

使用与 Storyboard 版本类似的结构,您可以创建带有按钮的基本视图控制器,然后在下面添加将成为应用程序新根"的视图.

Using a similar structure to the Storyboard version, you could create the base view controller with its button, and then, add the view that will become then new "root" of the application, underneath.

为了清楚起见,让我们将保存按钮的视图控制器称为 FakeRootViewController,并且对于所有实际目的而言,该视图控制器将成为应用程序的根目录:根视图控制器.所有后续的视图控制器甚至都不知道有 FakeRootViewController 高于其他人.

To make it clear, let's call the one view controller that holds the button the FakeRootViewController, and the view controller that will be, for all practical purposes, the root of the application: RootViewController. All subsequent view controllers won't even know that there's the FakeRootViewController above everyone else.

// The "real" root
#import "RootViewController.h"

// Call once after the view has been set up (either through nib or coded).
- (void)setupRootViewController
{
    // Instantiate what will become the new root
    RootViewController *root = [[RootViewController alloc] <#initWith...#>];

    // Create the Navigation Controller
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];

    // Add its view beneath all ours (including the button we made)
    [self addChildViewController:nav];
    [self.view insertSubview:nav.view atIndex:0];
    [nav didMoveToParentViewController:self];
}

AppDelegate.m

#import "FakeRootViewController.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    FakeRootViewController *fakeRoot = [[FakeRootViewController alloc] <#initWith...#>];

    self.window.rootViewController = fakeRoot;
    [self.window makeKeyAndVisible];

    return YES;
}

这样,您就可以享受在窗口上插入按钮的所有好处,而不会感到内疚和我真的应该成为一名程序员吗?"它导致.

That way, you can have all the benefits of inserting the button on the window, without all the guilt and "Should I really be a programmer?" that it causes.

这篇关于使按钮在所有视图控制器中持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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