开发者

iphone how to disable UIBarButtonItem

开发者 https://www.devze.com 2023-03-12 11:55 出处:网络
This question has been asked many times, but I tried every solution and none of them solved my problem.

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
                                                                              开发者_如何学Go  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.


Solution:

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

there is also:

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

works in iOS 5.


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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号