iphone如何禁用UIBarButtonItem

时间:2011-06-10 09:16:36

标签: iphone uibarbuttonitem

这个问题已被多次询问,但我尝试了所有解决方案,但没有一个解决了我的问题。

这是工具栏创建代码:

- (id)initWithBlogArticle:(BlogArticle *)theArticle
{
    if (self = [super init])
    {
        CustomToolbar* tools = [[CustomToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 48.01)];
        [tools setTintColor:[UIColor colorWithRed:0.62 green:0.70 blue:0.13 alpha:1.0]];

        NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

        UIBarButtonItem* previousArticle = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind
                                                                                         target:self
                                                                                         action:@selector(displayPreviousArticle)];

        [previousArticle setStyle:UIBarButtonItemStyleBordered];
        [buttons addObject:previousArticle];
        [previousArticle release];

        UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                target:nil
                                                                                action:nil];
        [buttons addObject:spacer];
        [spacer release];

        UIBarButtonItem *nextArticle = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward 
                                                                                     target:self
                                                                                     action:@selector(displayNextArticle)];
        [nextArticle setStyle:UIBarButtonItemStyleBordered];
        [buttons addObject:nextArticle];
        [nextArticle release];

        [tools setItems:buttons animated:NO];

        [buttons release];

        [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:tools] autorelease]];
        [tools release];

        [self setWebView:[[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
        [[self webView] setDelegate:self];
        [self loadBlogArticle:theArticle];

        [self setView:[self webView]];
    }

    return self;
}

我尝试过以各种方式使用setEnabled:NO并且它永远不会起作用,这让我发疯,因为禁用按钮应该是如此简单......所以这要么是非常复杂,要么我不是理解非常基本的东西。

请提前帮助,谢谢。

2 个答案:

答案 0 :(得分:6)

解决方案:

self.navigationItem.rightBarButtonItem.enabled  = NO/YES;

还有:

self.navigationItem.leftBarButtonItem.enabled  = NO/YES;

适用于iOS 5。

答案 1 :(得分:1)

Declare previousArticle and nextArticle in header file

- (id)initWithBlogArticle:(BlogArticle *)theArticle
{
if (self = [super init])
{
    CustomToolbar* tools = [[CustomToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 48.01)];
    [tools setTintColor:[UIColor colorWithRed:0.62 green:0.70 blue:0.13 alpha:1.0]];

    NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

    previousArticle = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind
                                                                                     target:self
                                                                                     action:@selector(displayPreviousArticle)];

    [previousArticle setStyle:UIBarButtonItemStyleBordered];
    [buttons addObject:previousArticle];


    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                            target:nil
                                                                            action:nil];
    [buttons addObject:spacer];
    [spacer release];

    nextArticle = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward 
                                                                                 target:self
                                                                                 action:@selector(displayNextArticle)];
    [nextArticle setStyle:UIBarButtonItemStyleBordered];
    [buttons addObject:nextArticle];


    [tools setItems:buttons animated:NO];

    [buttons release];

    [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:tools] autorelease]];
    [tools release];

    [self setWebView:[[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
    [[self webView] setDelegate:self];
    [self loadBlogArticle:theArticle];

    [self setView:[self webView]];
}

return self;
}

-(void)displayNextArticle
{
if(articleEnd)
nextArticle.enabled=NO; 
else
nextArticle.enabled=YES;    

}
-(void)dealloc
{
[previousArticle release];
[nextArticle     release];
}