0

这是情况。我有一个名为“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”类中的属性的任何事情都没有任何反应。

如果有人可以帮助我,我将不胜感激。

谢谢

4

2 回答 2

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;

第二步:在 IB 中绘制按钮,并将它们的类设置为 ColorSwatch。然后可以跳过我在viewDidLoad中写的代码。

第 3 步:新放置的按钮现在应该在 IB 中显示一个出口。您可以像往常一样将其拖到 MyViewController 中。

于 2012-07-25T14:40:10.943 回答
0

您的 IBOutlet 中可能存在连接问题photoLabelTextField,您可能忘记将xib文本字段与您的photoLabelTextField

于 2012-07-25T14:03:14.463 回答