IBAction doesn't connect to addTarget


I am really struggling right now, finally have understood solutions from this forum but my IBAction doesn’t connect to the addTarget message. Maybe one of you can help me out here. Would be much appreciated.

Here is my code:


#import <UIKit/UIKit.h>

@interface BNRHypnosisView : UIView

@property (strong, nonatomic) UIColor *circleColor;



[code]#import “BNRHypnosisView.h”

@implementation BNRHypnosisView

…cut out stuff

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
NSLog(@"%@ was touched", self);
float red = (arc4random() % 100) / 100.0;
float green = (arc4random() % 100) / 100.0;
float blue = (arc4random() % 100) / 100.0;

UIColor *randomColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
self.circleColor = randomColor;

[self setNeedsDisplay];




[code]#import “BNRHypnosisViewController.h”
#import “BNRHypnosisView.h”

@interface BNRHypnosisViewController()

@property (strong, nonatomic) BNRHypnosisView *backgroundView;


@implementation BNRHypnosisViewController

self.backgroundView = [[BNRHypnosisView alloc]init];
self.view = self.backgroundView;

UISegmentedControl *uControl = [[UISegmentedControl alloc]initWithItems:@[@"Red", @"Green", @"Blue"]];
uControl.frame = CGRectMake(35, 35, 250, 40);
uControl.backgroundColor = [UIColor whiteColor];
[self.view addSubview];

[uControl addTarget:self action:@selector(changeCircleColor: ) forControlEvents:UIControlEventValueChanged];


[super viewDidLoad];
NSLog(@“BNRHypnosisViewController loaded its view.”);

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
self.tabBarItem.title = @“Hypnotize”;
UIImage *i = [UIImage imageNamed:@“Hypno.png”];
self.tabBarItem.image = i;


return self;


-(IBAction)changeCircleColor:(UISegmentedControl *)segmentedControl
if(segmentedControl.selectedSegmentIndex == 0)
[self.backgroundView setCircleColor:[UIColor redColor]];
else if(segmentedControl.selectedSegmentIndex == 1)
[self.backgroundView setCircleColor:[UIColor greenColor]];
else if (segmentedControl.selectedSegmentIndex == 2)
[self.backgroundView setCircleColor:[UIColor blueColor]];



The key 'IBAction` is only useful for XIB files, as it gives a hint to Interface Builder that you want to hook this method up to an action triggered from the XIB file. Now, that shouldn’t hurt anything in what you are trying to do because IBAction is the same thing as void.

So based on the phrasing of your question, it sounds like the method -changeCircleColor: either isn’t being triggered, or isn’t doing what you want it to do when you change the value of the segmented color.

My guess based on looking at the code you’ve posted is that you don’t tell the BNRHypnosisView that is needs to redraw itself when the circleColor changes. But I’m not sure if that since you cut out some code. When the circleColor changes, we need to tell the view “hey, something has changed that impacts your drawing, so go ahead and redraw yourself when you can” and we do this by calling the -setNeedsDisplay method on the view.

We do this in the UIView chapter on p. 113 when we override the -setCircleColor: method. Could you take a look and make sure that the code on that page is in your BNRHypnosisView.m file?


Incredible. You figured out my mistake without seeing the code. Somehow I had deleted the accessor for circleColor in BNRHypnosisView.m. Many thanks for the quick reply! Great thing you have going here.