2012-07-25 3 views
0

여기 상황이 있습니다. "MyViewController"라는 제목의 뷰 컨트롤러가 있습니다. 이 뷰 컨트롤러에는 서브 클래 싱 된 버튼을 사용하는 텍스트 편집 기능이 있습니다. UIButton Subclass의 이름은 "ColorSwatch"입니다.UIButton 하위 클래스 대리자 메서드의 문제

다음과 같이 "ColorSwatch.h"하위 클래스에 설치 대리자/프로토콜 메소드가 있습니다. 내 "ColorSwatch.m"에서 지금

// 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 

내가 가진 다음 "MyViewController.h"에서

// 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.m"에서

// 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 

나는 have :

- (void)fontColor:(UIColor *)color 
{ 
    NSLog(@"Selected Font Color"); 
    [self.photoLabelTextField setTextColor:color]; 
} 

이제 대리자 메서드 인 sor t는 작동합니다. 즉, 색상 버튼을 누르면

NSLog(@"Selected Font Color"); 

메시지가 발생합니다. 그러나 문제는 변경할 수 없다는 것입니다.

[self.photoLabelTextField setTextColor:color]; 

속성입니다. 내가 속성을 변경하는 수많은 방법을 시도했다, 내가 할 수있는 유일한 일은 "MyViewController"클래스에서 속성을 변경하려고 아무것도 NSLogs를 보내는 것입니다.

누구든지 나를 도울 수 있다면, 고맙겠습니다.

당신이

답변

2

문제로 xib 텍스트 필드를 연결하는 ColorSwatch가 매달려에 위임 메시지를 보내는 것입니다 잊어 버린 수 있습니다 initWithCoder : 메소드에서 잘못 할당 한 MyViewController의 인스턴스.

UIControls는 ViewControllers를 대리인으로 할당해서는 안됩니다 ... 다른 방향으로 진행됩니다.

대신 코드에서 버튼을 건물의
- (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를 사용할 수 있습니다 ... MyViewController.m에, 그리고 ...

// in ColorSwatch.m initWithCoder: 
MyViewController *mvc = [[MyViewController alloc] initWithNibName: 
              @"MyViewController" bundle:nil]; 
self.colorSwatchDelegate = mvc; 

을이 라인을 삭제합니다.

1 단계 : 대리인 출구 만들 ...

@property (nonatomic, retain) IBOutlet id <ColorSwatchDelegate> colorSwatchDelegate; 

단계 2 : IB의 버튼을 그리고 ColorSwatch 그들의 클래스를 설정. 그러면 viewDidLoad에서 작성한 코드를 건너 뛸 수 있습니다.

3 단계 : 새롭게 배치 된 단추는 이제 IB에 콘센트를 제공해야합니다. 평상시처럼 MyViewController에서이 객체로 드래그 할 수 있습니다.

+0

이 작업을 수행 할 수 있는지 확인하려고하지만 내 단추는 Interface Builder에서 이미 배치되어 있습니다. 각 버튼의 클래스는 "ColorSwatch"로 설정됩니다. "MyViewController"파일의 코드에서 버튼을 할당하고 초기화해야한다고 말씀 하시겠습니까? – dana0550

+0

아니요. IB를 사용해도 괜찮습니다. 더 나은. 무한 루프가 보이지 않는 이유는 잘못 할당 한 VC가 느리게보기 때문에로드를로드하기 때문입니다. 결코보기를 요구하지 않으므로 루프를 시작하지 않습니다. – danh

+0

핵심은 버튼 초기화에서 VC 할당을 얻는 것입니다. – danh

0

하여 함께 IBOutlet photoLabelTextField에 연결 문제가있을 수 있습니다 감사합니다, 당신은 당신의 photoLabelTextField

+0

아니요.텍스트 필드가 올바르게 작동합니다. 메인 뷰 컨트롤러에서 아무런 문제없이 색을 바꿀 수 있습니다. UIButton Subclass에서 변경하려고하면 문제가 발생합니다. – dana0550