2

我正在尝试设置一个基于导航的应用程序,其中 UIButton (提供“关于”信息)作为导航栏上的标题。我可以轻松地将按钮设置为标题,但不知道应该在哪里做,所以它是永久性的(每次调用新的 viewController 时导航栏都不会滑动)。这是我当前的代码:

AppDelegate.h

    @class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;

@end

AppDelegate.m

    #import "AppDelegate.h"
    #import "ViewController.h"
    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.viewController = [[ViewController alloc]    initWithNibName:@"ViewController_iPhone" bundle:nil];
        } else {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
        }
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
        self.window.rootViewController = navController;
        [self.window makeKeyAndVisible];
        return YES;
    }

ViewController.m

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(100, 4, 120, 36);
        [button setImage:[UIImage imageNamed:@"logo_1.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(about) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.titleView = button;
        // Do any additional setup after loading the view, typically from a nib.
    }

为了澄清起见,“viewDidLoad”中的代码工作正常——我只想让它在开始时设置并永远持续下去(我在实际方法中使用单例没有问题——我只是不想重新加载它)。

我一直在挖掘该站点并修补了几个小时,但似乎无法使其正常工作;提前感谢您的帮助。

4

1 回答 1

0

每个UIViewController压入UINavigationController堆栈的都有它自己的UINavigationBar,所以没有办法有一个全局按钮。您可以为基类中的每个ViewControllers设置按钮创建一个通用基类viewDidLoad

我创建了一个示例应用程序来演示这种方法。

https://github.com/GayleDDS/SameButtonOnAllNavBars

在此处输入图像描述

于 2013-05-17T05:40:32.410 回答