Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4002256
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:00:16+00:00 2026-05-20T08:00:16+00:00

I have a UIView called baseView and in that I have initWithFrame where I

  • 0

I have a UIView called baseViewand in that I have initWithFramewhere I add some other views and do some custom stuff. The same view also has a NIB file.

Now I have a UIViewController class named AppController in which I want to add the baseView view to the view of the AppController view so I am doing this:

self.view = baseView; but the problem is that the NIB file does not get loaded. How do I make sure the customized stuff AND the NIB file get´s loaded/run?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:00:17+00:00Added an answer on May 20, 2026 at 8:00 am

    You have many options, depending on how your “baseView” class is meant to be used and integrated in to your application. It’s not clear just how you intend to use this class — as the view in a UIViewController subclass, or as a reusable modular component mean to be instantiated multiple times throughout your application, for use in many different view controllers.

    If your view is meant to be the only view in a UIViewController subclass, then Phonitive is correct — bundle it together with the UIViewController subclass .xib file and use the UIViewController’s viewDidLoad to do final initialization.

    But if you want your View class to be a subcomponent reused multiple times in different view controllers, integrated either via code or via inclusion in a .xib file for another controller, then you need to implement both the initWithFrame: init method, and awakeFromNib, to handle both cases. If your internal initialization always includes some objects from .xib, then in your initWithFrame you’ll need to load your .xib manually in order to support “customer” classes that want to create your widget via code. And likewise, if a .xib file contains your object then you’ll need to make sure you call any code-required finalization from awakeFromNib.

    Here’s an example of how to create a UIView subclass component with the UI design in a nib.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIView subclass that I am doing some custom drawing in. When
I have a custom UIView that generates a set of subviews and display them
I have a custom UIView called TiledImage which has a single property named tiledImage
I have a UIView subclass that draws itself when -drawRect: is called. It only
I have a UIView subclass called Card that I move around on my board
I have a UIView that will eventually contain about 2 screens worth of information.
If I have a UIView (or UIView subclass) that is visible, how can I
Currently, I have a UIView subclass that stamps a single 2px by 2px CGLayerRef
I have a subclass s of UIView. I want to put some buttons and
I have a UIView called activityView , which contains two subviews activityIndicator and cancelOperationsButton

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.