Programing

UIView 및 initWithFrame 및 NIB 파일.

lottogame 2020. 10. 14. 07:18
반응형

UIView 및 initWithFrame 및 NIB 파일. NIB 파일을로드하려면 어떻게해야합니까?


내가 가지고 UIView전화 baseView와 내가이 점에서 initWithFrame나는 다른 의견을 추가하고 일부 사용자 지정 물건을 할 경우. 동일한보기에는 NIB 파일도 있습니다.

이제 뷰의 뷰에 뷰를 추가하고 싶은 UIViewController클래스가 있으므로 다음을 수행합니다.AppControllerbaseViewAppController

self.view = baseView;그러나 문제는 NIB 파일이로드되지 않는다는 것입니다. 사용자 정의 된 항목과 NIB 파일이로드 / 실행되는지 어떻게 확인합니까?


"baseView"클래스를 사용하고 애플리케이션에 통합하는 방법에 따라 많은 옵션이 있습니다. UIViewController 하위 클래스의 뷰 또는 재사용 가능한 모듈 식 구성 요소로서이 클래스를 사용하려는 방법은 명확하지 않습니다. 즉, 여러 뷰 컨트롤러에서 사용하기 위해 응용 프로그램 전체에서 여러 번 인스턴스화됩니다.

뷰가 UIViewController 하위 클래스의 유일한 뷰인 경우 Phonitive가 정확합니다. UIViewController 하위 클래스 .xib 파일과 함께 번들로 묶고 UIViewController의 viewDidLoad를 사용하여 최종 초기화를 수행합니다.

그러나 View 클래스가 다른 뷰 컨트롤러에서 여러 번 재사용되고 코드를 통해 통합되거나 다른 컨트롤러의 .xib 파일에 포함되도록하려면 initWithFrame : init 메서드와 awakeFromNib를 모두 구현해야합니다. 두 경우 모두 처리합니다. 내부 초기화에 항상 .xib의 일부 개체가 포함되어있는 경우 코드를 통해 위젯을 생성하려는 "고객"클래스를 지원하기 위해 initWithFrame에서 수동으로 .xib를로드해야합니다. 마찬가지로 .xib 파일에 개체가 포함되어있는 경우 awakeFromNib에서 코드가 필요한 모든 마무리를 호출해야합니다.

다음은 펜촉의 UI 디자인을 사용하여 UIView 하위 클래스 구성 요소를 만드는 방법의 예입니다.

MyView.h :

@interface MyView : UIView
{
    UIView *view;
    UILabel *l;
}
@property (nonatomic, retain) IBOutlet UIView *view;
@property (nonatomic, retain) IBOutlet UILabel *l;

MyView.m :

#import "MyView.h"
@implementation MyView
@synthesize l, view;

- (id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code.
        //
        [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil];
        [self addSubview:self.view];
    }
    return self;
}

- (void) awakeFromNib
{
    [super awakeFromNib];

    // commenters report the next line causes infinite recursion, so removing it
    // [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil];
    [self addSubview:self.view];
}

- (void) dealloc
{
     [l release];
     [view release];
     [super dealloc];
}

Here's what the nib file looks like (except that file's owner needs to be changed to MyView class).

enter image description here

be sure to hook up both the view and label outlets to File's Owner. That's it! A template for creating re-usable UIView widgets.

The really neat thing about this structure is that you can place instances of your MyView object in other nib files, just place a UIView at the location/size you want, then change the class in the identity inspector (CMD-4) to MyView, and boom, you've got an instance of your widget in whatever views you want! Just like UIKit objects you can implement delegate protocols so that objects using your widget can be notified of interesting events, and can provide data to display in the widget to customize it.


I found this post after having a problem trying to do this in my app. I was trying to instantiate the view from a NIB in the ViewDidLoad method, but the controls acted as if they were disabled. I struggled with this trying to directly set the userInteractionEnabled property and programmatically set the touch event selector for a button in this view. Nothing worked. I stumbled upon another post and discovered that viewDidLoad was probably too soon to be loading this NIB. I moved the load to the ViewWillAppear method and everything worked. Hope this helps someone else struggling with this. The main response was great and works well for me now that I have it being called from the proper place.


if you want to use a NIB, it's better for your UIView to be linked with a UIViewController, in this case you can use

UIViewController *vc=[[UIViewController alloc] initWithNibName:@"YourNIBWihtOUTEXTENSION" bundle:nil]

[self.view addSubView:vc.view ];

becareful of memory leaks, you have to release vc


If you have a custom UIView with a xib file.

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

   id mainView;
   if (self)
   {
       NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"HomeAllAdsView" owner:self options:nil];
       mainView = [subviewArray objectAtIndex:0];    
   }
   return mainView;
}

- (void) awakeFromNib
{
   [super awakeFromNib];
}

This post helped me Building Reusable Views With Interface Builder and Auto Layout. The trick had to do with setting the IBOutlets to the FileOwner and then adding the content view to itself after loading the nib

참고URL : https://stackoverflow.com/questions/5056219/uiview-and-initwithframe-and-a-nib-file-how-can-i-get-the-nib-file-loaded

반응형