setToolbarItems问题

时间:2011-04-21 06:37:06

标签: iphone cocoa-touch

我使用以下代码在基于导航的应用程序的屏幕上添加了工具栏

    //Create an array to hold the list of bar button items
    NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:3];


    //Add buttons

    //load the image
    UIImage *buttonImage ;


    buttonImage = [UIImage imageNamed:@"test.png"];



    //create the button and assign the image for window width and level
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(WWL:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:buttonImage forState:UIControlStateNormal];
    //set the frame of the button to the size of the image (see note below)
    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
    //create a UIBarButtonItem with the button as a custom view
    WindowWidthZoom = [[UIBarButtonItem alloc]  initWithCustomView:button  ];
    [items addObject:WindowWidthZoom ];

    [self setToolbarItems:items];
    [[self navigationController] setToolbarHidden:NO animated:NO];

但是当离开屏幕时,我注意到工具栏没有在其他屏幕上消失,任何建议如何在离开此屏幕之前隐藏它以避免其出现在其他屏幕中,以及如何将其颜色更改为黑色< / p>

2 个答案:

答案 0 :(得分:1)

您可以将以下内容添加到所有其他视图控制器的-viewWillAppear方法中:

[self.navigationController setToolbarHidden:YES animated:NO];

在要显示工具栏的视图控制器中,确保在-viewWillAppear方法中也将setToolbarHidden设置为NO。在-viewDidLoad方法中这样做是不够的,因为每次出现视图时都不会调用此方法。例如,当您点击导航控制器的“后退”按钮并返回到上一个视图控制器时,由于该视图控制器已经加载,因此可能不需要再次加载(因此不会调用-viewDidLoad。)

答案 1 :(得分:0)

请更改您的上一行代码:

 [[self navigationController] setToolbarHidden:NO animated:NO];

为:

 [self setToolbarHidden:NO animated:NO];

并确保将其调用:

- (void)viewDidAppear:(BOOL)animated;