我正在尝试设置一个基于导航的应用程序,其中 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”中的代码工作正常——我只想让它在开始时设置并永远持续下去(我在实际方法中使用单例没有问题——我只是不想重新加载它)。
我一直在挖掘该站点并修补了几个小时,但似乎无法使其正常工作;提前感谢您的帮助。