0

This question has been asked many times, but I tried every solution and none of them solved my problem.

Here's the toolbar creation code :

- (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;
}

I've tried to use setEnabled:NO in every possible way and it never works, which is driving me crazy as disabling a button should be so simple...so either this is extremely complicated to do or I'm not understanding something very basic.

Please help, thanks in advance.

Whitewall
  • 597
  • 1
  • 7
  • 18
teum
  • 125
  • 3
  • 12

2 Answers2

6

Solution:

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

there is also:

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

works in iOS 5.

Costique
  • 23,712
  • 4
  • 76
  • 79
lolo_house
  • 69
  • 1
  • 4
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];
}
nambi
  • 547
  • 3
  • 10
  • I had already tried this solution before but it didn't work and now it does, I don't understand, maybe I did something wrong...but it's all good now, thanks. – teum Jun 10 '11 at 11:40
  • I have a question though, if you don't mind answering it : do I have to move the release of the buttons elsewhere (viewDidUnload + dealloc) now that they belong to the class ? I think I have to do that, please confirm. – teum Jun 10 '11 at 11:47
  • Yes we have to release the buttons(previous and next) in dealloc. – nambi Jun 10 '11 at 12:26