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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:55:02+00:00 2026-05-25T16:55:02+00:00

I am pretty new to iPhone programming, and was playing around with an app

  • 0

I am pretty new to iPhone programming, and was playing around with an app yesterday trying different scenarios with view controllers and nib files. So, I started a new app with a FirstViewController (FVC for short) and an FVC.xib.

I layed out a quick view in FVC.xib and ran the app – view displays, great.

I now wanted to have a second view I could add on top of the main view. So I went ahead and created SecondViewController.xib (SVC) but did not create the .m and .h files. I went about trying to load both these views from the same view controller, and here is where my question lies:

I created a button in FVC.xib and created an IBAction like this:

- (IBAction)loadSVC {
    FirstViewController *viewController = [[FirstViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
    secondView = viewcontroller.view;
    [viewController release];
    [self.view addSubView:secondView];
}

So this works great and adds the contents of SVC.xib, but when I try and remove that view from the superview, the app crashes:

[secondView removeFromSuperview];

If I actually create a view controller for SVC, use that to instantiate my view in FVC, and move the remove code to the SVC:

[self.view removeFromSuperview];

Everything works. My question – I kind of get why my first method crashes, but I was hoping someone could explain why and what goes on behind the scenes. I’m still a noob with object oriented programming, so what is actually happening in my first case where I create a new instance of FirstViewController and add its view to self.view? Why can’t I release it (I assume because the original view is associated with FirstViewController, and when I create a new instance with the second xib it messes everything up) – I’d love a more technical explanation as to what is happening…

Thanks much!!

EDIT to add more info in response to Nick’s reply below

Nick – so your answer did clear my thinking a bit in regards to the retain count, etc… I did another test app trying to get this working from a single view controller – think, for example, that I wanted to display an Alert or Welcome message to the user (I know in a real app there are different methods to accomplish this, but this is more of a learning experience) — so I have my main view @ MainViewController and layout my alert message in a xib called alert.xib — so there is no logic behind the alert message, no reason for it to have a view controller that I can see, my end goal being loading/unloading this on top of my main view from the main view’s view controller (or understanding why it is impossible)

I tried this using instance variables as you recommended:

In MainViewController.h:

#import <UIKit/UIKit.h>

UIViewController *secondController;
UIView *secondView;

@interface MainViewController : UIViewController {

}

@property(nonatomic, retain) UIViewController *secondController;
@property(nonatomic, retain) UIView *secondView;

- (IBAction)loadSecond;
- (IBAction)removeSecond;

@end

In MainViewController.m:

#import "MainViewController.h"

@implementation MainViewController

@synthesize secondController, secondView;

- (IBAction)loadSecond {
secondController = [[MainViewController alloc] initWithNibName:@"alert" bundle:[NSBundle mainBundle]];
secondView = secondController.view;
[self.view addSubview:secondView];
}

- (IBAction)removeSecond {
//I've tried a number of things here, like [secondView removeFromSuperview];, [self.secondView removeFromSuperview];, [secondController.view removeFromSuperview];
}
- (void)dealloc {
[secondController release];
[secondView release];
[super dealloc];
}

So – this works to load the alert view, but the removeSecond button does nothing (I did use NSLog to verify the removeSecond method is fired) – why?

Second, and most importantly – is this even possible, or is it horrible practice? Should every nib/view I am manipulating have their own view controller? Am I wrong to think I could just make a new instance of MainViewController and use it to display and remove this no-functionality, very temporary view? (And yes, I realize I could easily create this view programatically or accomplish the end goal in many different ways which would be easier, but I’m trying to really learn this stuff and I think figuring this out will help…

Thanks for the 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-25T16:55:02+00:00Added an answer on May 25, 2026 at 4:55 pm
    1. Based on your clarifications, you don’t need secondView property or iVar. Also in your loadSecond instead of secontController = bla you need self.secondController = bla, otherwise you simply assign reference to the iVar instead of going through the setter.
    2. Yes, it’s possible to load subviews/other resources from a nib without having a dedicated controller

    This is how you do it (one of the approaches):

    UIView *result = nil;
    NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:@"MyNibName" owner:owner options:nil];
    for ( id o in bundle ) {
        if ( [o isKindOfClass:[UIView class]] ) {
            result = (UIView *)o;
            break;
        }
    }
    

    Here the result will contain the first UIView in MyNibName. You can use other criteria to find out whether you got the view you wanted (tags, types…)

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

Sidebar

Related Questions

I m pretty new to iPhone development. I'm trying to implement scroll view with
I'm pretty new to iPhone development but I'm close to releasing my first app
I'm pretty new to iPhone development. I am building an app which has multiple
I'm pretty new to iPhone development and I'm still getting my head around Objective-C,
I'm pretty new to iphone programming. I have come up with a problem when
I am pretty new to iPhone/Objective-c programming. I'm struggling with an issue that doesn't
I'm pretty new to iPhone development. I have my root view and it is
I am pretty new to iphone programming therefore I apologize if my question could
Well I am pretty new to both iphone and PHP development, and am trying
I'm pretty new to iPhone programming, so I'm looking for any pointers (no pun

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.