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

  • Home
  • SEARCH
  • 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 7440963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:56:01+00:00 2026-05-29T10:56:01+00:00

Why does this init method return an object out of scope? Using XCode 4.2,

  • 0

Why does this init method return an object out of scope?

Using XCode 4.2, base SDK of 4.3, and ARC, I’m trying to load an UIView from a nib (not a UIViewController). I need to not use a UIViewController at all in the process.

After reading this answer to an S.O. question here, it looks like it can be done:
How to load a UIView using a nib file created with Interface Builder
(The answer by user “MusiGenesis” describes the process)

I created a sub-class of UIView with a single label:

@interface MyView : UIView
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *textLabel;
@end

In the implementation I override initWithFrame:

- (id)initWithFrame:(CGRect)frame
{
    //self = [super initWithFrame:frame];
    self = [JVUIKitUtils initWithNibName:@"MyView" withOwner:self];
    if( self )
    {
        NSLog(@"Created");
    }
    return self;
}

In I.B. I created a file named “MyView.xib” with a single view. It has a label as a sub-view, and I created the label property by dragging it to the h file.
MyView nib file setup

And in another file, I created this re-usable static method:

+ (id)initWithNibName:(NSString*)nibName withOwner:(id)uiView
{
    id object = nil;
    NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:nibName owner:uiView options:nil]; // 1 object, out of scope
    for( id tempObject in bundle)
    {
        if( [tempObject isKindOfClass:[uiView class]] ) object = tempObject;
        break;
    }
    return object;
}

As you can see in the following screen shot, the bundle has one object reference, but it’s out of scope.

And debugging:
MyView is out of scope

This is my code for instantiation:

subView = [[MyView alloc] initWithFrame:CGRectZero]; // ok
NSAssert(subView != nil, @"MyView was nil"); // fail

Any ideas on why the other S.O. poster was able to get it to work but this does not?

  • 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-29T10:56:02+00:00Added an answer on May 29, 2026 at 10:56 am

    The use of the owner seems a bit confusing in the way that you are loading a nib. It appears that you are trying to use the view as both the owner of the nib and the first object in it.

    Are you trying to load MyView from your nib (i.e. is the class of the view inside your nib files defined as MyView) or are you trying to load a subview of MyView from the nib?

    If the view inside your nib is a MyView, here’s how to load it. Create this static method as a category on UIView:

    @implementation UIView (NibLoading)
    
    + (id)viewWithNibName:(NSString*)nibName
    {
        NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:nibName owner:nil options:nil];
        if ([bundle count])
        {
            UIView *view = [bundle objectAtIndex:0];
            if ([view isKindOfClass:self])
            {
                return view;
            }
            NSLog(@"The object in the nib %@ is a %@, not a %@", nibName, [view class], self);
        }
        return nil;
    }
    
    @end
    

    That will let you load any kind of view from a nib file (the view needs to be the first item defined in the nib). You would create your view like this:

    MyView *view = [MyView viewWithNibName:@"MyView"];
    

    If the view inside the nib is not a MyView, but you want to load it as a subview of MyView, with MyView defined as the file’s owner in the nib file, do it like this:

    @implementation UIView (NibLoading)
    
    - (void)loadContentsFromNibName:(NSString*)nibName
    {
        NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
        if ([bundle count])
        {
            UIView *view = [bundle objectAtIndex:0];
            if ([view isKindOfClass:[UIView class]])
            {
                //resize view to fit
                view.frame = self.bounds;
    
                //add as subview
                [self addSubview:view];
            }
            NSLog(@"The object in the nib %@ is a %@, not a UIView", nibName, [view class]);
        }
    }
    
    @end
    

    Using that approach, just create your view as normal using initWithFrame, then call loadContentsFromNibName to loa the contents from a nib. You would load your view like this:

    MyView *view = [[MyView alloc] initWithFrame:CGRect(0,0,100,100)];
    [view loadContentsFromNibName:@"MyView"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does this pattern: setTimeout(function(){ // do stuff }, 0); Actually return control to the
Why does this javascript return 108 instead of 2008? it gets the day and
Been using a Copy method with this code in it in various places in
I am trying to make NSTableDataSource compatible object and give this object to NSTableView
Does this look like it should work? I'm wanting to generate directions from one
Does this smell? I have a few properties you can only set once. They
Does this code cause a memory leak: int main(){ int * a = new
Does this seem right, the dataFilePath is on disk and contains the right data,
What does this mean exactly? I'm doing something like this: File.Copy(@\\foo\bar\baz.txt, @c:\test\baz.txt); MSDN doesn't
Why does this lambda expression not compile? Action a = () => throw new

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.