2013-04-09 36 views
0

我創建了一個導航控制器,但我不能設置標題或添加按鈕,導航bar.How這樣做呢? 這是應用DidFinishLauchingOption的文件AppDelegate.m代碼:設置標題,並添加按鈕,導航控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:view]; 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 
    self.view = [[ViewController alloc] init]; 
    self.window.rootViewController = self.view; 
    [self.window addSubview:navController.view]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

在此先感謝。

回答

0

您需要將窗口對象的rootViewController屬性設置爲導航控制器,而不是你的ViewController的`實例。這應該指向你在正確的方向:

@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Create and configure a window 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.window.backgroundColor = [UIColor whiteColor]; 

    // Create a view controller 
    UIViewController *viewController = [[ViewController alloc] init]; 

    // Create a navigation controller and set its root view controller to the instance of `ViewController` 
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 

    // Add the navigation controller to the window 
    self.window.rootViewController = navController; 

    [self.window makeKeyAndVisible]; 

    return YES; 
} 

// ... 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Set the view controller's title 
    self.title = NSLocalizedString(@"View Controller", @""); 

    // Add a navigation bar button 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshButtonPressed:)]; 
} 

- (void)refreshButtonPressed:(id)sender 
{ 
    // Do something when the refresh button is pressed. 
} 

// ... 

@end 
+0

它不工作。我放入我的視圖控制器的所有東西都沒有出現。 :( – lncnb91 2013-04-09 16:13:35

+0

哦,我只試了一次有更多的時間和它的作品。謝謝! – lncnb91 2013-04-09 16:21:27

0

要創建初始設置,爲您打造一個導航控制器與您的視圖控制器,並將其設爲您的應用程序代理窗口的根視圖控制器:

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

    //create and set root view controller 
    [[self window] setRootViewController:[[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]]]; 

    //make window key and visible 
    [self.window makeKeyAndVisible]; 

    //bail 
    return YES; 
} 

然後在您的視圖控制器,設置標題,並添加您的導航項目:

- (void)viewWillAppear:(BOOL)animated 
{ 
    //call parent implementation 
    [super viewWillAppear:animated]; 

    //set view controller title 
    [self setTitle:@"Root View Controller"]; 

    //add navigation bar button 
    [[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Button Title" style:UIBarButtonItemStyleBordered target:self action:@selector(handleBarButtonItemEvents:)]]; 
} 

,並通過監聽按鈕事件:

- (void)handleBarButtonItemEvents:(id)sender 
{ 
    // 
} 
+0

謝謝,它的工作原理。 – lncnb91 2013-04-09 16:21:48

相關問題