Search code examples
iosobjective-cxcodeuibuttonuisegmentedcontrol

Obj C ui segmented control to de/activate a button


everyone,

I can't get a segmentedcontrol in an older Objective c app to activate or deactivate another button.

I have tried the following so far: I have linked the button and SegmentedControl to the same IBAction. SegmentedControl -> Sent Event: -> Value Changed -> File's Owner: clearHighScore: Button -> Sent Event: -> Touch Up Inside -> File's Owner clearHighScore:

HighScoreViewController.m :

#import "HighScoreViewController.h"
@synthesize segmentedControl;    

- (IBAction)clearHighScore:(id)sender {

        if (self.segmentedControl.selectedSegmentIndex == 0) {}
        else if (self.segmentedControl.selectedSegmentIndex == 1) {
                deletes.A.table;
        }
    }

HighScoreViewController.m :

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

- (IBAction)clearHighScore:(id)sender;

@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;

I also tried to link the SegmentedControl to a different IBAction in HighScoreViewController.m : (I know that it would be reduntand)

- (IBAction)activateClearHighScoreButton:(id)sender {
    switch (self.segmentedControl.selectedSegmentIndex) {
        case 0:
            self.segmentedControl.selectedSegmentIndex = 0;
            break;
        case 1:
            self.segmentedControl.selectedSegmentIndex = 1;
            break;
        default:self.segmentedControl.selectedSegmentIndex = 0;
            break;
    }
}

- (IBAction)clearHighScore:(id)sender {

    if (self.segmentedControl.selectedSegmentIndex == 0) {}
    else if (self.segmentedControl.selectedSegmentIndex == 1) {
            deletes.A.table;
    }
}

I think I am missing the value transfer into (IBAction)clearHighScore but I don't know how to solve this problem.

(The reason to do this is to have a invisible button which deletes data but is only clickable/active if the invisible switch was clicked. Alternatively, I would like to have a password prompted by clicking on the button, after which the data will be deleted. However, I have even less idea how to implement this.)

Thanks in advance


Solution

  • You need to set it up as follows.

    • Connect your button to outlet clearButton.
    • Connect your segmentedControl to outlet segmentedControl
    • Connect the buttons Touch Up Inside to clearHighScore
    • Connect the segmentedControl's valueChanged to activateClearHighScoreButton

    The button will now be enabled/disabled when you tap on the segmented control.

    @property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
    @property (weak, nonatomic) IBOutlet UIButton *clearButton;
    
    - (IBAction)clearHighScore:(id)sender {
        if (self.segmentedControl.selectedSegmentIndex == 0) {}
        else if (self.segmentedControl.selectedSegmentIndex == 1) {
                deletes.A.table;
        }
    }
    
    - (IBAction)activateClearHighScoreButton:(id)sender {
        switch (self.segmentedControl.selectedSegmentIndex) {
            case 0:
                _clearButton.enabled = NO;
                break;
            case 1:
                _clearButton.enabled = YES;
                break;
            default:
                break;
        }
    }