2012-08-12 1 views
0

그래서 두 개의 뷰 MyViewMySubview이 있고 두 개의 서브 클래스는 모두 UIView입니다.사용자 정의 UIView 서브 클래 싱 - 초기화 방법

View.h :

@interface MyView : UIView <ProgressCallback> 
@property (retain, nonatomic) IBOutlet UIProgressBar *progress; 

- (id) initWithFrame: (CGRect) frame; 
- (void) progressUpdated: (NSNumber) newProgress; 

@end 

View.m

@implementation MyView 
@synthesize progress = _progress; 

- (id)initWithFrame:(CGRect)frame; 
{ 
    self = [super initWithFrame:frame]; 

    if (self){ 
     NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"MyViewNibFile" 
                   owner:nil 
                  options:nil]; 

     if ([arrayOfViews count] < 1){ 
      [self release]; 
      return nil; 
     } 

     View *newView = [[arrayOfViews objectAtIndex:0] retain]; 
     [newView setFrame:frame]; 

     [self release]; 
     self = newView; 

     self.progress.progress = .25 // This will work and change it. 
    } 

    return self; 
} 

- (void) progressUpdated: (NSNumber) newProgress { 
    self.progress.progress = newProgress.floatValue; // This also works! 
} 

나는 애플의 문서 및 StackOverflow의를 탐색하여 UIView의 서브 클래스를 만드는이 방법을 발견했다. 그것은 작동, 콜백을 기반으로 진행 상황을 설정할 수 있습니다, 등 나타납니다. 내 문제는 내가 이것을 subview하고 싶을 때입니다.

MySubview.h

@interface MySubview : MyView 
@property (retain, nonatomic) IBOutlet UIImageView *image; 
@end 

MySubview.m

@implementation의 MySubview

- (id)initWithLabel: (UIImageView *) image andWithFrame:(CGRect)frame; 
{ 
    self = [super initWithFrame:frame]; 

    if (self){ 
     NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"MySubViewNibFile" 
                   owner:nil 
                  options:nil]; 

     if ([arrayOfViews count] < 1){ 
      [self release]; 
      return nil; 
     } 

     MySubbiew *newView = [[arrayOfViews objectAtIndex:0] retain]; 
     [newView setFrame:frame]; 

     [self release]; 
     self = newView; 

     self.progress.progress = .25 // This will work and change it. 
     self.image = image; // Also works 
    } 

    return self; 
} 

아무리 내 서브 클래스를 초기화하는 방법, 나는 서브 클래스에서 작업 진행률 표시 줄을 얻을 수 없습니다 . 나는 init 동안 그것을 설정할 수있다. 그리고 그것은 nil이 아니지만 init 이후에 내 view에있는 것과 분명히 일치하지 않는다. 나는 그것이 스스로를 공개한다는 사실과 관련이 있다고 생각하지만, 유형 View의 클래스를 반환하고 싶다면 다른 대안을 볼 수 없다. 나는 같은 스타일의 init을 사용한다. 단지 클래스 이름을 Subview로 바꾸기 만하면된다. 콜백은 실제로 호출되고 코드가 변경되지만 표시를 업데이트하지는 않습니다. 모든 것은 펜촉에 정확하게 연결되어 있으며, 크기와이 경우에는 이미지를 제외하고는 동일합니다.

는 (필자는 예제 코드는 간단하고 가능한 한 명확하게하기 위해 최선을 시도했다. 내가 성공하지 않은 경우, 나는 그것을 업데이 트됩니다, 알려 주시기 바랍니다.)

을 당신에게 제대로 서브 클래스 UIView의 작업을 수행하는 방법 ? 나는 문서를 읽었지만 계몽 적이라는 것이 입증되지는 않았지만, 저를 올바른 방향으로 가르쳐 주시기 바랍니다. 특히, 모든 항목이 올바른 항목을 가리 키도록하려면 어떤 항목을 재정의해야합니까? 즉, 디스플레이에있는 것입니다.

당신은 initWithLabel:andWithFrame: 방법 initWithFrame:를 호출 한 후 자기의 가치를 재정의
+0

코코아 터치는 코코아와 같지 않으므로이 질문을 적절히 편집해서는 안됩니다. –

+0

self = newView; 이것은 상속을 부패시키지 않는가?나중에 MySubview를 하위 클래스로 만들려면 이니셜 라이저에서 만들려는 새 클래스 대신 MySubview 클래스를 returen합니다. –

답변

0

이 라인 : 당신이 잃을 수 있도록

self = newView; 

에서, MYVIEW의 슈퍼 클래스의 init 메소드에서 만든 자기의 값을 대체합니다 모든 사용자 정의

0

이 문제가 발생하면 내가 잘못했기 때문에 잘못했을 수도 있습니다. 일반적으로

:

콘텐츠 덤비는 경우, UIViewController 우선합니다.

지오메트리가 엉망인 경우 UIView을 무시하십시오.

기술적 인면에서 내 코드가 잘못되었지만 wattson12가 말한대로이 문제가 해결되었습니다.