0

I would like to use UIButton as a switch, first tap as switch on, and second tap as switch off - and onwards.

I read a stackoverflow article and made a new class ToggleButton as described. By having kind advises from stack overflow, no error messages appeared on coding level. However when running the program, pressed the toggleButton and was terminated by the error "[ViewController ToggleOn:]: unrecognized selector sent to instance".

Any further coding improvements would be required?

My sample codes are follows.

//ToggleButton.h:

@interface ToggleButton : UIButton
@property (nonatomic, getter=isOn) BOOL on;
-(void)toggle;
@end


//ToggleButton.m:

#import "ToggleButton.h"
@interface ToggleButton ()
@property (nonatomic, strong) UIImage *offStateImage;
@property (nonatomic, strong) UIImage *onStateImage;
-(void)touchedUpInside:(UIButton*) sender;
@end

@implementation ToggleButton
@synthesize on = _on;
@synthesize offStateImage = _offStateImage;
@synthesize onStateImage = _onStateImage;

-(void)awakeFromNib
{
[super awakeFromNib];

self.offStateImage = [self imageForState:UIControlStateNormal];
self.onStateImage = [self imageForState:UIControlStateHighlighted];

[self addTarget:self
         action:@selector(touchedUpInside:)
forControlEvents:UIControlEventTouchUpInside];
}


-(void)touchedUpInside:(UIButton*) sender
{ [self toggle]; }

-(void)toggle
{self.on = (!_on);}

-(void)setOn:(BOOL) on
{
_on = on;

if (on)
    [self setImage:self.onStateImage forState:(UIControlStateNormal)];
else
    [self setImage:self.offStateImage forState:(UIControlStateNormal)];
}

@end


//  ViewController.h`
#import <UIKit/UIKit.h>
#import "ToggleButton.h"

@interface ViewController : UIViewController
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *ai;
@property (nonatomic, retain) IBOutlet UISwitch *sw;
@property (nonatomic, retain) IBOutlet ToggleButton *tb;
@end


//ViewController.m

#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController
@synthesize ai;
@synthesize sw;
@synthesize tb;

- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

//IBSwitch implementation

- (IBAction)SwitchChanged:(id)sender {
if(sw.on){    
[ai startAnimating];

}else{

[ai stopAnimating];}

}


//ToggleButton implementation

- (IBAction)ToggleButton:(id)sender {

if(tb.on){

    [ai startAnimating];

}else{

    [ai stopAnimating];}
}
@end
Community
  • 1
  • 1
Santos
  • 1
  • 2

2 Answers2

1

Add a button on interface builder and set the custom class to ToggleButton (3rd tab from the left).

import ToggleButton.h in ViewController.h and add this line

@property (nonatomic, retain) IBOutlet ToggleButton *tb;

Connect tb and - (IBAction)ToggleButton:(id)sender to Button on interface builder.

Replace this method in your ToggleButton.m class

-(void)toggle
{self.on = (_on);}

to

-(void)toggle
{self.on = (!_on);}

now it will work fine.

nicael
  • 18,550
  • 13
  • 57
  • 90
Agam
  • 44
  • 4
0

If you are using interface builder: set the custom class to ToggleButton, as Paulw said.

If you are creating the button in the code, you can just simply create a new instance of ToggleButton class in your view controller.

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156