这是情况。我有一个名为“MyViewController”的视图控制器。在这个视图控制器中,我有一个使用子类按钮的文本编辑功能。UIButton 子类的名称是“ColorSwatch”
我在“ColorSwatch.h”子类中设置了委托/协议方法,如下所示。
// 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
现在在我的“ColorSwatch.m”中,我有:
// 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
在“MyViewController.h”中,我有:
// 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
在“MyViewController.m”中,我有:
- (void)fontColor:(UIColor *)color
{
NSLog(@"Selected Font Color");
[self.photoLabelTextField setTextColor:color];
}
现在委托方法有点工作,这意味着当我点击颜色按钮时
NSLog(@"Selected Font Color");
消息被触发。但问题是我无法改变
[self.photoLabelTextField setTextColor:color];
财产。我尝试了多种更改属性的方法,我唯一能做的就是发送 NSLogs,我尝试更改“MyViewController”类中的属性的任何事情都没有任何反应。
如果有人可以帮助我,我将不胜感激。
谢谢