2011-06-10 84 views
0

這個問題已被問了很多次,但我試過每個解決方案,他們都沒有解決我的問題。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來無微不至,它永遠不會奏效,這是推動我瘋狂的禁用按鈕應該是這麼簡單..所以要麼做起來非常複雜,要麼我不瞭解非常基本的東西。

請幫助,在此先感謝。

+1

應該setEnabled:NO來,我嘗試在模擬器上,它的工作。 你在哪裏禁用按鈕? – arkchong 2011-06-10 09:35:02

+0

你有沒有試過這個:UIBarButtonItemName.enabled = NO; – 2011-06-10 09:30:30

回答

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

我以前已經試過這個解決方案,但它沒有工作,現在它確實,我不明白,也許我做錯了什麼......但現在一切都很好,謝謝。 – teum 2011-06-10 11:40:44

+0

雖然我有一個問題,如果你不介意回答它:我必須將其他地方的按鈕釋放(viewDidUnload + dealloc),現在他們屬於這個類了嗎?我想我必須這樣做,請確認。 – teum 2011-06-10 11:47:23

+0

是的,我們必須釋放dealloc中的按鈕(上一個和下一個)。 – nambi 2011-06-10 12:26:10

6

解決方案:

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

還有:

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

作品的iOS 5

+0

最簡單的和那個爲我工作的。謝謝。 – Ali 2012-10-07 16:35:22