Re: controlling buttons
Re: controlling buttons
- Subject: Re: controlling buttons
- From: Fritz Anderson <email@hidden>
- Date: Fri, 7 Sep 2001 11:23:48 -0500
On Friday, September 7, 2001, at 09:56 AM, Mark Wridt wrote:
>
I have a two button app that needs to be controlled in
>
a specific way,that is, button1 MUST be pressed before
>
button2, and once button2 is pressed, then it cannot
>
be pressed again.
>
>
I can't seem to find anyting in NSButton or
>
NSButtonCell.
>
>
Can you have an action event inside of an action
>
event?
You have to provide the logic for your application in a controller
object you implement yourself. The controller can keep track of the
application state and ensure the UI reflects it.
enum TwoControllerState {
k2CInitial, k2COnePressed, k2CTwoPressed
};
@interface TwoController : NSObject
{
IBOutlet id button1;
IBOutlet id button2;
enum TwoControllerState state;
}
// button1's action message:
- (IBAction) button1Pressed: (id) sender;
// button2's action message:
- (IBAction) button2Pressed: (id) sender;
// action message for a "Reset" menu item:
- (IBAction) reset: (id) sender;
@end
@implementation TwoController
// Private: accept a new state and make sure the
// UI reflects / enforces it
- (void) setState: (enum TwoControllerState) newState
{
state = newState;
switch (newState) {
case k2CInitial:
[button1 setEnabled: YES];
[button2 setEnabled: NO];
break;
case k2COnePressed:
[button2 setEnabled: YES];
break;
case k2CTwoPressed:
[button2 setEnabled: NO];
break;
}
}
// Private: set the controller and the UI to the initial state:
- (void) awakeFromNib { [self setState: k2CInitial]; }
- (IBAction) reset: (id) sender { [self setState: k2CInitial]; }
- (IBAction) button1Pressed: (id) sender {
if (state == k2CInitial)
[self setState: k2COnePressed];
}
- (IBAction) button2Pressed: (id) sender {
[self setState: k2CTwoPressed];
}
@end