2013-02-19 1 views
2

CustomView.xib 파일을 사용하여 CustomView : UIView를 만듭니다. 이제 뷰를 다른 XIB (예 : UIViewController.xib)로 드래그하여 사용하고 class : customView를 선택하려고합니다.다른 XIB에서 클래스를 지정할 때 CustomView.xib로 CustomView를로드하는 방법?

내가 anotherView에있는 CustomView와 addSubView init을 때이 성공적으로로드 할 수 있습니다 경우

- (void)viewDidLoad{ 
    //Success load NIB 
    CustomView *aView = [[CustomView alloc] initWithFrame:CGRectMake(40, 250, 100, 100)]; 
    [self.view addSubview:aView]; 
} 

//CustomView.m

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     NSLog(@"INIT"); 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     [[nib objectAtIndex:0] setFrame:frame]; 
     self = [nib objectAtIndex:0]; 
    } 
    return self; 
} 

지정한 다음 다른 XIB로 그리기에 의해 CustomCell을 재사용 클래스를 CustomView로 설정합니다. awakeFromNib가 호출되었지만 CustomView.xib를로드하는 방법을 알지 못합니다. 어떻게 그럴 수 있습니까?

* 편집 :

initWithCoder도 클래스를 지정하지만 loadNibNamed와 루프와 충돌을 생성 할 때 호출된다. 왜?

- (id)initWithCoder:(NSCoder *)aDecoder{ 
    if (self = [super initWithCoder:aDecoder]) { 
     NSLog(@"Coder"); 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"QSKey" owner:nil options:nil]; 
     [[nib objectAtIndex:0] setFrame:self.bounds]; 
     self = [nib objectAtIndex:0]; 
    } 
    return self; 
} 
+0

당신이 실제로 원하는 것을 ... 당신을받지 ?? –

+0

CustomView를 xib와 함께로드하고 initWithFrame을 사용하지 않고 다른 Xib로 뷰를 드래그 한 다음 class.But을 지정하면 XIB가로드되지 않습니다. –

+0

나는 이것이 네가하고 싶은 일이라고 생각 하나? http://stackoverflow.com/a/5096476/840428 – followben

답변

1

은 직접 당신이 한 일을 그 슈퍼 클래스 뷰를 드래그 할 수 있습니다 possible.But하고 배치 할 객체에 속성을 연결 class.And 사용자 정의에 해당 클래스를 설정하지 뷰 컨트롤러에서 사용자 정의 XIB를 드래그 귀하의 사용자 정의 클래스에 따라. 것입니다 여기에

QSKey *keyView=[[QSKey alloc] init]; 
    NSArray *topObjects=[[NSBundle mainBundle] loadNibNamed:@"QSKey" owner:self options:nil]; 

for (id view in topObjects) { 
    if ([view isKindOfClass:[QSKey class]]) { 
     keyView=(QSKey*)view; 
    } 
} 

[email protected]"CView"; 
[keyView setFrame:CGRectMake(0, 0, 100, 100)]; 
[keyView setBackgroundColor:[UIColor redColor]]; 
[self.view addSubview:keyView]; 

here label is UILabel object you have used in your custom class as its property. 


//Load custom View using drag down objects of type Custom Class Super class. 

for (QSKey *aView in self.view.subviews) { 
    if ([aView isKindOfClass:[QSKey class]]) { 
     [self addGestureRecognizersToPiece:aView]; 
     [email protected]"whatever!"; 
    } 
} 

Here label is object of UILabel you have to place on your dragged View to view controller's xib view.Change that view's class to your Custom Class.Then it will will show its label property in outlet connect it to your UILabel object placed over dragged view. 

가 작동하는 코드 TestTouchonView

입니다 ....

//로드 XIB 사용하여 코드와 스크린 샷 :

직접 다음 코드를 사용 XIB 사용자 정의를로드 할 수 있습니다 다음과 같이 표시하십시오.

enter image description here

관련 문제