2014-10-28 5 views
2

Swift 프로젝트에서 xib의 구성 요소를 재사용하려고하지만 Objective C 논리를 사용하고 있습니다. 따라서 뷰를 서브 클래스 화하고 xib를로드 한 다음 사용자 정의 뷰보기 컨트롤러에 인스턴스화합니다.Swift에서 뷰 하위 클래스의 xib로드

코드를 Swift로 변환하면 예상 결과가 생성되지 않습니다.


CustomView.swift :

override init(frame: CGRect) { 
    super.init(frame: frame) 
} 

required init(coder aDecoder: NSCoder) { 

    super.init(coder: aDecoder) 
    fatalError("Error detected") 

    self.commonInit() 
} 

private func commonInit() { 
    NSBundle.mainBundle().loadNibNamed("mainBar", owner: self, options: nil) 
    self.addSubview(self) 
} 

viewController.swift :

var bottomBar : customView = customView(frame: CGRect(x: 0, y: 250, width: 250, height: 70)) 
    //bottomBar.backgroundColor = UIColor.redColor() 
self.view.addSubview(bottomBar) 

Objectiv I가 기준이되는 E-C : 우측 방향으로 임의의 주석이 이해된다

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self baseInit]; 
    } 
    return self; 
} 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 

[[NSBundle mainBundle] loadNibNamed:@"yourXib" owner:self options:nil]; 

    [self addSubview:self.view]; 
    } 
    return self; 
} 

CustomView * myView = [CustomView alloc]init]; 
    [self.view addSubView:myView]; 

.

+0

실행에 신속한 코드로 문제가 무엇을 시도 할 수 있습니다? – Woodstock

+0

xib가 뷰 컨트롤러에로드되지 않았습니다. – carlodurso

+0

사용 방법 - 답변 : https://stackoverflow.com/questions/9282365/load-view-from-an-external-xib-file-in-storyboard# – Shaky99

답변

2

OBJ-C 코드가 실제로 작동하는지는 100 % 확신 할 수 없지만 실제로는 그렇지 않습니다. 이 [[NSBundle mainBundle] loadNibNamed:@"yourXib" owner:self options:nil];은 항상 IB xib 파일에 개체 배열을 반환한다는 것을 알아야합니다. 실제로이 객체를 내가 보는 곳 어디에도 할당하지 않습니다. 이 구현의 문제점은 경계에서 취할 수있는 프레임 일 수 있습니다. 이 코드는 IB 개체에 개체가 하나만 있다고 가정하면 작동합니다.

private func commonInit() 
{ 
    var nibView = NSBundle.mainBundle().loadNibNamed("mainBar", owner: self, options: nil)[0] as UIView 
    nibView.frame = self.bounds; 
    self.addSubview(nibView) 
} 
+0

답변 해 주셔서 감사합니다. 당신은 당신의 초기 가정에 맞습니다. Objective-C에서이 코드를 테스트하지 않았습니다. 내가 제공 한 코드로는 실패했지만 UIView 하위 클래스에서 펜촉을 초기화 할 수 없다는 느낌이 들었습니다. 비슷한 코드가'UIViewController'에서 서브 클래 싱 할 때 작동합니다. 당신은 내가 [사용자 정의 탭 막대 라이브러리] (http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom- 탭 표시 줄 /)을 신속하게? – carlodurso

+0

코드가 맞다면, 아마도 IB와 다른 방식으로 작업했을 것입니다. 클래스 유형의 파일 소유자를 만들고 IB와 함께 제공되는 UIView (또는 다른 기본 객체)가 원래 그대로 있도록하는 것입니다. 도움이되는지 알려주세요. – Vanya

+0

제 의견에 동의합니다. 실제로 xib를 사용하는 대신 프로그래밍 방식으로 단추를 추가하는 UIView 클래스를 작성하여 해결할 수있었습니다. – carlodurso

2

당신은이

override init(frame: CGRect) { 
    super.init(frame: frame) 
    loadViewFromNib() 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    loadViewFromNib() 
} 

func loadViewFromNib() { 

    let view = UINib(nibName: "CreditCardExperyView", bundle: NSBundle(forClass: self.dynamicType)).instantiateWithOwner(self, options: nil)[0] as! UIView 

    view.frame = bounds 

    view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] 

    self.addSubview(view); 

} 

// Call subview 
let creditCardView : CreditCardExperyView = CreditCardExperyView(frame: CGRect(x: 0, y: UIScreen.mainScreen().bounds.size.height - 280, width: UIScreen.mainScreen().bounds.size.width, height: 280)) 

selfView.addSubview(creditCardView) 
관련 문제