여기 상황이 있습니다. "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를 보내는 것입니다.
누구든지 나를 도울 수 있다면, 고맙겠습니다.
당신이
이 작업을 수행 할 수 있는지 확인하려고하지만 내 단추는 Interface Builder에서 이미 배치되어 있습니다. 각 버튼의 클래스는 "ColorSwatch"로 설정됩니다. "MyViewController"파일의 코드에서 버튼을 할당하고 초기화해야한다고 말씀 하시겠습니까? – dana0550
아니요. IB를 사용해도 괜찮습니다. 더 나은. 무한 루프가 보이지 않는 이유는 잘못 할당 한 VC가 느리게보기 때문에로드를로드하기 때문입니다. 결코보기를 요구하지 않으므로 루프를 시작하지 않습니다. – danh
핵심은 버튼 초기화에서 VC 할당을 얻는 것입니다. – danh