为所有ViewControllers定制导航控制器

时间:2011-11-29 14:34:01

标签: iphone uinavigationcontroller uinavigationbar

我的AppDelegate.h

//
//  AppDelegate.h
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UIWindow *window;
    UITabBarController *tabBarController;
}

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBarController;

@end

我的AppDelegate.m

//
//  AppDelegate.m
//
#import "AppDelegate.h"

#import "AppNavigationController.h"

#import "ExamViewController.h"
#import "SignsViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (void)dealloc
{
    [_tabBarController release];
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];

    // Initializating our Tab Bar Controller
    tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

    // Exam Controller Setup
    ExamViewController *examViewController = [[ExamViewController alloc] initWithStyle:UITableViewStylePlain];
    AppNavigationController *examNavigationController = [[AppNavigationController alloc] initWithRootViewController:examViewController];

    examViewController.title = @"Экзамен";
    examViewController.tabBarItem.image = [UIImage imageNamed:@"icon_exam.png"];
    // -------------------------------------

    // Signs Controller Setup
    SignsViewController *signsViewController = [[SignsViewController alloc] initWithStyle:UITableViewStylePlain];
    AppNavigationController *signsNavigationController = [[AppNavigationController alloc] initWithRootViewController:signsViewController];

    signsViewController.title = @"Знаки";
    signsViewController.tabBarItem.image = [UIImage imageNamed:@"icon_signs.png"];

    // -------------------------------------

    [tabBarController  setViewControllers:[NSArray arrayWithObjects:examNavigationController, signsNavigationController, nil]];

    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];

    [examNavigationController release];
    [examViewController release];

    [signsNavigationController release];
    [signsViewController release];

    return YES;
}

我也有空的UINavigationController。

我想要实现的是为使用它的所有viewcontrollers定制的导航栏 例如:现在我只有两个viewcontrollers,现在我将self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;放在ExamViewController.m和SignsViewController.m中的viewDidLoad方法中 但可能我想添加两个或更多选项卡,并希望在一个地方自定义导航栏。 所以问题是:如何在一个地方自定义导航栏以在每个viewcontroller中看起来相同而不在viewDidLoad方法中的每个viewcontroller中自定义它?

2 个答案:

答案 0 :(得分:3)

试试这个:。

  1. 创建一个新的视图控制器,比如说ViewControllerTemplate.h
  2. 将设计设置为.m文件中的ViewControllerTemplate导航控制器
  3. 然后,每次要创建一个视图控制器时,只需将ViewControllerTemplate.h设置为其超类,它将继承该设计。

    ViewControllerTemplate *newController = [[ViewControllerTemplate alloc]init];
    

答案 1 :(得分:1)

如果您使用的是iOS 5,则新的方法是UIAppearence。见这里:Custom Appearance for UIKit Controls

这是另一个很棒的教程,如何在iOS 5中自定义UI,由Steve Baranski: User Interface Customization in iOS 5