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 5845875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:29:50+00:00 2026-05-22T12:29:50+00:00

I have a nib file I’m trying to instantiate in code. My UIViewController’s main

  • 0

I have a nib file I’m trying to instantiate in code. My UIViewController’s main view is also loaded from a nib file.

Here’s my UIViewController’s viewDidLoad method:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UINib *nib = [UINib nibWithNibName:@"MyCustomView" bundle:nil];
    NSArray *nibViews = [nib instantiateWithOwner:self options:nil];
    MyCustomView *myView = [nibViews objectAtIndex:0];
    myView.frame = CGRectMake(100.0f, 100.0f, 91.0f, 91.0f);
    [self.view addSubview:myView];
}

This creates some sort of endless loop. If I comment out [self.view addSubview:myView], myView appears, but everything currently on the screen disappears. I didn’t think instantiateWithOwner added the view to the screen. If it does, how do I get access to it?

Thanks for your help.

  • 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-22T12:29:51+00:00Added an answer on May 22, 2026 at 12:29 pm

    The instantiateWithOwner method reassigns the properties of your view controller (set from the nib the controller was created from) to ones from the new nib. Those properties likely include the view property, so that method, within it, contains a call to setView:, and sets the view controller’s view to the new nib’s view. Afterwards, you’re trying to add a view as a subview to itself, and that, naturally, causes problems.

    You want to create your own property, for instance, secondaryView, set the nib’s view to that, and add it as a subview. You don’t want to reassign your view controller’s view.

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

Sidebar

Related Questions

I have a nib file where I have a view that contains a background
I have a nib file I've loaded like so [NSBundle loadNibNamed: @myNibFile owner: self
I have a Nib file containing grouped table view and a cell. The cell
I have a View Controller inside my MainWindow.xib file that loads the nib Cover
I have my main app view controller (File's Owner), its First Responder, View, and
I'm trying to have my view hierarchy loaded through calls to loadNibNamed. For a
I have a strange issues. I have a nib file with some view inside
I am trying to create 3 table view cells using code (w/o nib). I
I have a NIB file with a UIView, a couple of UIImageView's (including a
I have a NIB file that I load using NSWindowController initWindowNibName. The NIB file

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.