2012-07-25 67 views
0

這裏是情況。我有一個名爲「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」類中的屬性的東西都不會發生。

如果有人可以請幫助我,我將不勝感激。

謝謝

回答

2

問題連接的是,ColorSwatch正在發送委託消息懸空在它的initWithCoder:方法中錯誤分配的MyViewController實例。

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。

+0

我打算看看能否讓這個工作,但僅供參考,我的按鈕已經在Interface Builder中進行了佈局。每個按鈕的類別都設置爲「ColorSwatch」。你是說我應該在「MyViewController」文件中的代碼中分配和初始化按鈕嗎? – dana0550 2012-07-25 16:11:10

+0

不需要,可以使用IB。甚至更好。我認爲你沒有看到一個無限循環的原因是因爲你錯誤地分配的VC加載它的觀點懶惰。既然你從不要求它的觀點,它不是開始循環。 – danh 2012-07-25 16:22:05

+0

關鍵是從您的按鈕init中獲取該VC分配。 – danh 2012-07-25 16:22:47

0

有可能是連接問題,在您的IBOutlet中photoLabelTextField,你可能已經忘記了xib文本字段您photoLabelTextField

+0

不,不是。文本字段正常工作。我可以在主視圖控制器中改變顏色而沒有任何問題。當我嘗試從UIButton子類中改變它時,問題就出現了。 – dana0550 2012-07-25 14:33:48