2013-04-09 35 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

相关问题