情况如下。我有一个名为“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”类中更改属性没有任何反应。
如果有人能帮助我,我会很感激。
谢谢
答案 0 :(得分:2)
问题是ColorSwatch将委托消息发送到MyViewController的悬空实例,它在initWithCoder:方法中错误地分配了它。
UIControls不应该将ViewControllers分配给他们的代表......反之亦然。
删除这些行...
// in ColorSwatch.m initWithCoder:
MyViewController *mvc = [[MyViewController alloc] initWithNibName:
@"MyViewController" bundle:nil];
self.colorSwatchDelegate = mvc;
然后,在MyViewController.m ...
- (void)viewDidLoad {
ColorSwatch *colorSwatchButton = [[ColorSwatch alloc] buttonWithType:UIButtonTypeCustom];
// or place a ColorSwatch in the xib, on MyViewController's view... But not before you
// you delete lines from initWithCoder, otherwise it's infinite circular allocation
colorSwatchButton.frame = CGRectMake(/* ... */);
colorSwatchButton addTarget:self action:@selector(csButtonPressed:) forControlEvent: UIControlEventTouchUpInside];
// and so on...
// now the important part:
colorSwatchButton.colorSwatchDelegate = self;
// see - the ViewController is in charge of allocation, sets itself up as the delegate
[self.view addSubview:colorSwatchButton];
}
您可以使用IB。
,而不是在代码中构建按钮第1步:让代表成为出口......
@property (nonatomic, retain) IBOutlet id <ColorSwatchDelegate> colorSwatchDelegate;
步骤2:在IB中绘制按钮,并将其类设置为ColorSwatch。 然后你可以跳过我在viewDidLoad中写的代码。
步骤3:新放置的按钮现在应该在IB中显示出口。您可以像往常一样从那里拖动到MyViewController。
答案 1 :(得分:0)
您的IBOutlet photoLabelTextField
可能存在连接问题,您可能忘记将xib
文字字段与photoLabelTextField