导航控制器工具栏按钮问题 - Xcode

时间:2012-03-22 11:22:47

标签: ios xcode uinavigationcontroller toolbar

我正在尝试在navigationController的工具栏中添加一些按钮:我看到工具栏,但没有按钮。这是我设置工具栏的代码部分......

(这是我的AppDelegate)

// Create a table view controller
    RootViewController *rootViewController = [[RootViewController alloc]
                                              initWithStyle:UITableViewStyleGrouped];

    rootViewController.managedObjectContext = context;
    rootViewController.entityName = @"County";

    //Navigation Controller
    UINavigationController *aNavigationController = [[UINavigationController alloc]
                                                     initWithRootViewController:rootViewController];

    self.navigationController = aNavigationController;


    //Barbuttons
    UIBarButtonItem *homeButton;
    homeButton = [[[UIBarButtonItem alloc] initWithTitle:@"         Inizio         " style:UIBarButtonItemStyleBordered target:self action:@selector(home)] autorelease];

    UIBarButtonItem *barButton;
    barButton = [[[UIBarButtonItem alloc] initWithTitle:@"  Funzioni online   " style:UIBarButtonItemStyleBordered target:self action:@selector(caricamappa)] autorelease];

    UIBarButtonItem *creditsButton;
    creditsButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"credits2.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(credits)] autorelease];    

    NSArray *baritems = [NSArray arrayWithObjects: homeButton, barButton, creditsButton, nil];

    [window addSubview:[navigationController view]];
    [self.navigationController.toolbar setItems:baritems];
    [self.navigationController setToolbarHidden:NO];


    [window makeKeyAndVisible];

    [rootViewController release];
    [aNavigationController release];

关于我的错误的任何想法?

2 个答案:

答案 0 :(得分:1)

您应该将按钮添加到rootViewController的navigationItem属性,而不是导航控制器的工具栏。 类似的东西:

rootViewController.navigationItem.rightBarButtonItems = barItems;

答案 1 :(得分:0)

查看the documentation,尤其是此部分:

  

此工具栏的内容管理是通过自定义视图完成的   与此导航控制器关联的控制器。对于每个视图   在导航堆栈上的控制器,您可以分配一组自定义   工具栏项使用setToolbarItems:animated:方法   的UIViewController。

相关问题