更改enabled属性后,UIBarButtonItem停止工作

时间:2012-07-01 21:06:07

标签: iphone objective-c ios5

我有一个通过界面构建​​器连接到以下方法的UIBarButton:

- (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,但不会正确设置当前页面,并且不会显示该卡。奇怪的是,按几下按钮后,它可能会起作用。

很间歇......

感谢您提供的任何帮助

2 个答案:

答案 0 :(得分:0)

尝试将-(void)btnJumpToStart_Touch:(id)sender设置为-(IBAction)btnJumpToStart_Touch:(id)sender,如@Popeye所述。如果这不起作用,请尝试在控制器的UIBarButton中以编程方式创建和添加viewDidLoad

答案 1 :(得分:0)

好的,所以我注释掉了启用/禁用控件的代码。然后我做了它,以便在IB中启用按钮。

我在模拟器中玩过产品。 ONce我已经完成了,我取消注释了该代码,然后将按钮设置为在开始时被禁用。现在它有效......