Here is the situation. I have a view controller titled “MyViewController.” Within this view controller I have a text editing feature that uses subclassed buttons. The name of the UIButton Subclass is “ColorSwatch”
I have setup delegate/protocol methods in the “ColorSwatch.h” subclass as follow.
// ColorSwatch.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
@protocol ColorSwatchDelegate <NSObject>
- (void)fontColor:(UIColor *)color;
@end
@interface ColorSwatch : UIButton {
id <ColorSwatchDelegate> colorSwatchDelegate;
CAGradientLayer *gradient;
UIView *currentView;
UIColor *fontColor;
}
@property (nonatomic, retain) id <ColorSwatchDelegate> colorSwatchDelegate;
@property (nonatomic, retain) CAGradientLayer *gradient;
@property (nonatomic, retain) UIView *currentView;
@property (nonatomic, retain) UIColor *fontColor;
@end
Now in my “ColorSwatch.m” I have:
// ColorSwatch.m
#import "ColorSwatch.h"
#import <QuartzCore/QuartzCore.h>
#import "MyViewController.h"
@implementation ColorSwatch
@synthesize gradient;
@synthesize currentView;
@synthesize colorSwatchDelegate;
@synthesize fontColor;
-(void)setupView{
"Makes the subclassed buttons pretty"
}
-(id)initWithFrame:(CGRect)frame{
if((self = [super initWithFrame:frame])){
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if((self = [super initWithCoder:aDecoder])){
[self setupView];
MyViewController *mvc = [[MyViewController alloc] initWithNibName:
@"MyViewController" bundle:nil];
self.colorSwatchDelegate = mvc;
}
return self;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self magnify:view];
fontColor = view.backgroundColor;
[self.colorSwatchDelegate fontColor:fontColor];
}
- (void)magnify:(UIView *)view
{
}
- (void)dealloc
{
[currentView release];
[gradient release];
[fontColor release];
[super dealloc];
}
@end
In the “MyViewController.h” I have:
// MyViewController.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "ColorSwatch.h"
@interface MyViewController : UIViewController <ColorSwatchDelegate> {
UITextField *photoLabelTextField;
}
@property (nonatomic, retain) IBOutlet UITextField *photoLabelTextField;
@end
In the “MyViewController.m” I have:
- (void)fontColor:(UIColor *)color
{
NSLog(@"Selected Font Color");
[self.photoLabelTextField setTextColor:color];
}
Now the delegate method sort of works, meaning when I tap on a color button the
NSLog(@"Selected Font Color");
message gets fired. But the problem is that I cannot change the
[self.photoLabelTextField setTextColor:color];
property. I have tried numerous ways of changing the property, the only thing that I can do is send NSLogs, anything I try to change a property in the “MyViewController” Class nothing happens.
If anyone could please help me out, I would appreciate it.
Thank you
The problem is that the ColorSwatch is sending delegate messages to a dangling instance of MyViewController that it incorrectly allocated in it’s initWithCoder: method.
UIControls shouldn’t allocate ViewControllers to be their delegates… it goes the other way around.
Delete these lines…
Then, in MyViewController.m …
Instead of building the button in code, you can use IB.
Step 1: make the delegate an outlet…
Step 2: draw the buttons in IB, and set their class to ColorSwatch.
Then you can skip the code I wrote in viewDidLoad.
Step 3: The newly placed button should now present an outlet in IB. You can drag from that to the MyViewController as you normally do.