2012-07-01 62 views
0

後,我有一種連接在UIBarButton,通過接口生成器,下面的方法:的UIBarButtonItem停止工作,我改變enabled屬性

- (void)btnJumpToStart_Touch:(id)sender { 
    index = 0; 
    [self setCurrentPage]; 
    [self showCard]; 
} 

index在實施初期定義。 setCurrentPage是這樣的:

- (void)setCurrentPage { 

    // I need to set the bottom page control first, 
    // this allows me to display that the user is viewing the 
    // first half of the deck or the second 
    if(index < 11) { 
     [self.bottomPageControl setCurrentPage:0]; 
    } 
    else { 
     [self.bottomPageControl setCurrentPage:1]; 
    } 

    // now we set the top page control. I use the index that is being displayed, 
    // then divided by whether or not the bottom page control is showing the 
    // first half, or the second 
    [self.topPageControl setCurrentPage:(index - ((deck.count/2) * self.bottomPageControl.currentPage))]; 

    // next I set the 'jump to end/jump to start' button's enabled properties 

    // the jump to end will only be anabled when the 
    // index is less than the deck's count - 1 
    if(index < ([deck count] - 1)) { 
     [self.btnJumpToEnd setEnabled:YES]; 
    } 
    else { 
     [self.btnJumpToEnd setEnabled:NO]; 
    } 

    // the jump to start will only be enabled when the 
    // index is greater than 0 
    if(index > 0) { 
     [[self btnJumpToStart] setEnabled:YES]; 
    } 
    else { 
     [[self btnJumpToStart] setEnabled:NO]; 
    } 

} 

最後,showCard是這樣的:

- (void)showCard { 
    Card *card = [deck cardAtIndex:index]; 
    [cardImage setImage:[UIImage imageNamed:card.imageFile]]; 
} 

現在,你可以看到,btnJumpToStart將被禁用,或已啓用,在setCurrentPage方法。它將從殘疾人開始(我在IB中將其設置爲這樣)。當符合條件以'啓用'按鈕時,它不能正常工作。 index將被設置爲0,但它不會正確設置當前頁面,並且卡片不會顯示。奇怪的是,按幾次按鈕後,它可能會工作。

非常間歇...

感謝您的幫助,您可以提供

+0

我相信這行 - (void)btnJumpToStart_Touch:(id)發件人應改爲 - (IBAction)btnJumpToStart_Touch:(id)發件人。如果您在IB中設置了按鈕,那麼當您將新動作連接到按鈕時,您需要將方法設置爲IBActions。 – Popeye

+0

@Popeye不,只要它在.h中聲明IBOutlet,就沒關係。 Void和IBOutlet是彼此的typedef。 – CodaFi

+0

IBAction解析爲「無效」,IBOutlet解析爲無,但它們表示Xcode和Interface構建器可以在「接口」構建器中使用這些變量和方法來將UI元素鏈接到您的代碼。 如果你根本不打算使用Interface Builder,那麼你不需要它們在你的代碼中,但是如果你打算使用它,那麼你需要爲在IB中使用的方法指定IBAction, IB將用於IB中的對象。因此,如果您在Interface Builder中將此方法鏈接起來,則需要將該方法聲明爲IBAction。 – Popeye

回答

0

嘗試爲@Popeye提到設置-(void)btnJumpToStart_Touch:(id)sender-(IBAction)btnJumpToStart_Touch:(id)sender。如果這不起作用,請嘗試以編程方式在控制器的viewDidLoad中創建並添加UIBarButton

+0

是的,唯一的問題是,當我註釋掉setCurrentPage中處理啓用/禁用按鈕的部分時,我沒有問題... –

+0

調用'btnJumpToStart_Touch:'的地方或者什麼? – KDaker

0

好的,所以我註釋了啓用/禁用控件的代碼。然後我做了這個,以便在IB中啓用這些按鈕。

我在模擬器中玩過產品。 ONce我完成了,我沒有註釋掉那些代碼,然後在開始時將按鈕設置爲禁用。現在它的作品...