Enabling/Disabling a button
I am new to iPhone programming and when I use this code to enable/disable a button nothing happens. I connected my button through an IBOutlet and I know when I disable it in viewDidLoad it works. I just cant figure out why this method isn't working.
- (void) multiplicationButtonPressed {
if (multiplicationIsPressed) {
multiplicationButton.enabled = NO;
} else {
开发者_如何转开发 multiplicationButton.enabled = YES;
}
}
Please Check that your IBOutlet had connected properly for this button or not and then
use this piece of code
- (IBAction) multiplicationButtonPressed
{
multiplicationButton.selected = !multiplicationButton.selected;
}
That's it. You don't need to have any boolean flag. It works like toggle.
Please Check that your IBOutlet had connected properly for this button or not and then
Use this code
- (IBAction) multiplicationButtonPressed
{
if (multiplicationIsPressed) {
multiplicationButton.userInteractionEnabled = NO;
} else {
multiplicationButton.userInteractionEnabled = YES;
}
}
There are any number of reasons this could be failing. Without more context, we can only guess.
- If
multiplicationButtonPressed
is called before the view is loaded, the button hasn't been created yet. - You say
multiplicationButton
is hooked up as an IBOutlet in IB, but you could be wrong. Is it actually connected? - If
multiplicationButtonPressed
is the action for a button or other control, it's probably not actually hooked up to whatever it is supposed to be hooked up to. This is particularly likely since you declared it as(void)
rather than(IBAction)
, so unless the declaration in the .h file doesn't match IB won't even see it. - If (as the name implies)
multiplicationButtonPressed
is the action formultiplicationButton
itself, it will never be called when the button is not currently enabled because a button withenabled
set to NO will never respond to touches. - Or the same problem could exist somewhere higher up the view hierarchy: no subview of a view with
userInteractionEnabled
set to NO can respond to touches either. - Or it is possible that your layout is screwed up, and whatever button is supposed to be triggering this event is not actually within the bounds of all of its parent views. If said parent view doesn't clip to bounds, you could still see the button but not interact with it.
精彩评论