這裏是情況。我有一個名爲「MyViewController」的視圖控制器。在這個視圖控制器中,我有一個使用子類按鈕的文本編輯功能。 UIButton子類的名稱是「ColorSwatch」UIButton子類委託方法問題
我已經在「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];
}
現在委託方法sor作品的T,當我在彩色按鈕
NSLog(@"Selected Font Color");
消息被炒魷魚挖掘的意思。但問題是我無法更改
[self.photoLabelTextField setTextColor:color];
屬性。我嘗試了很多方法來改變屬性,我唯一能做的就是發送NSLogs,任何我試圖改變「MyViewController」類中的屬性的東西都不會發生。
如果有人可以請幫助我,我將不勝感激。
謝謝
我打算看看能否讓這個工作,但僅供參考,我的按鈕已經在Interface Builder中進行了佈局。每個按鈕的類別都設置爲「ColorSwatch」。你是說我應該在「MyViewController」文件中的代碼中分配和初始化按鈕嗎? – dana0550 2012-07-25 16:11:10
不需要,可以使用IB。甚至更好。我認爲你沒有看到一個無限循環的原因是因爲你錯誤地分配的VC加載它的觀點懶惰。既然你從不要求它的觀點,它不是開始循環。 – danh 2012-07-25 16:22:05
關鍵是從您的按鈕init中獲取該VC分配。 – danh 2012-07-25 16:22:47