0

My app needs to record a time interval by setting a start and stop time.

I want only the start button enabled at first, with the stop button being disabled.

On clicking the start button, I want the start button disabled (the following code accomplishes this fine), but I want this click to also enable the stop button. Clicking the stop button would render the stop button disabled (again, the following code handles this part as well).

I would also like to create a reset button that would return the start and stop buttons to their initial state - that is, the start button enabled and the stop button disabled, but I am pretty sure I can figure that out on my own if I get an answer to my initial query. I'm using Xcode 5 if that matters.

Thanks in advance!

    - (IBAction)startButton:(UIButton *)sender
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"hh:mm:ss.SS"];
        _startTimeLabel.text = [formatter stringFromDate:[NSDate date]];
        UIButton *startButton = (UIButton *)sender;
        startButton.enabled = NO;
    }

   - (IBAction)stopButton:(UIButton *)sender
   {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"hh:mm:ss.SS"];
        _stopTimeLabel.text = [formatter stringFromDate:[NSDate date]];
        UIButton *stopButton = (UIButton *)sender;
        stopButton.enabled = NO;
    }
1
  • give the tag of each UIButton in Xib or storyboard and all button action is convert into common button action and use the switch case,try this Commented Jan 1, 2014 at 14:29

4 Answers 4

2

If you add outlets as it was suggested in one of the previous answer (https://stackoverflow.com/a/20869697/3151066) for start and stop button your solution could look like this:

- (IBAction)startButton:(UIButton *)sender
{
    _startTimeLabel.text = [[self createDateFormatter] stringFromDate:[NSDate date]];
    self.start.enabled = NO;
    self.end.enabled = YES;
}


- (IBAction)stopButton:(UIButton *)sender
{
    _stopTimeLabel.text = [[self createDateFormatter] stringFromDate:[NSDate date]];
    self.stop.enabled = NO;
    self.start.enabled = YES;
}

You use exactly the same formatter twice so maybe you should consider refactoring this fragment of code to some method. Anyway it would be even better to have an instance variable for that formatter to not create it every time user click start or stop button as NSDateFormatter is a bit heavy component (remember then to initialise this date formatter before user could click start or stop button).

- (NSDateFormatter *) createDateFormatter 
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"hh:mm:ss.SS"];
    return formatter;//I assume you are using ARC so we do not need to take care of releasing memory
}

To have a reset button setting the initial state you will have to add another button as you did for start end stop buttons and add a following code:

- (IBAction)resetButton:(UIButton *)sender
{
   [self setInitialState];
}

- (void) setInitialState 
{
    self.start.enabled = YES;
    self.end.enabled = NO;
    //probably here you will also want to reset text for labels _startTimeLabel and _stopTimeLabel
}
Sign up to request clarification or add additional context in comments.

3 Comments

This handles the reset button. Thanks!
It is fixing a problem that you had after clicking each button (you were not enabling second button) ;) as you mention in another answer setInitialState can be called in viewDidLoad. You can also initialise there some instance of NSDateFormatter and use it instead of createDateFormatter method. @user2872997 I think you have an answer to your question so maybe you should mark the best one as it will be easier for others while looking for the solution ;)
@C_Dub is that an answer to your question? if so please mark it to make it clearer for future answer seeker, thanks
0

Create outlets from your class to the start and stop buttons and use those to set their states rather than hoping that sender is a button.

Comments

0

You'll want to add an IBOutlet for both the stop button and the start button:

@property (strong, nonatomic) IBOutlet UIButton start;
@property (strong, nonatomic) IBOutlet UIButton stop;

then, in Interface builder, connect each button to the respective outlet.

add

@synthesize start, stop;

into your implementation.

then, from within the StartButton method, you can add

stop.enabled = YES;

and similar for StopButton

start.enabled = YES;

1 Comment

Perfect! I was still trying to figure out how to set it up so that the stop button started out disabled, but I figured it out via one of the other answers here, setting the initial state in viewDidLoad. I would post code but I am on my phone. Nothing like coding an app when the only working Internet is on my phone :/ Thanks!
0

After create buttons use to boolean values for enable and disable

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//create button through add subview

Enable:

button.enable=YES; or button.enable=TRUE;

Disable:

button.enable=NO or button.enable=FALSE;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.