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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:11:53+00:00 2026-05-13T15:11:53+00:00

Without using Interface builder or xib files, what is the correct way to instantiate

  • 0

Without using Interface builder or xib files, what is the correct way to instantiate two classes which inherit from UIView such that they can switch between themselves using UIButtons located on the views themselves?

I think this involves setting up a UIViewController from the app delegate and adding two instances of my classes which implement UIView into the controller (perhaps from inside the controller?)

I’m also not sure how to raise events from UIButtons on the custom UIViews to switch the views. I suspect I would need to add a method to the view controller but I’m not sure how to get a reference to the view controller from inside the scope of my UIView.

Also, I’m wondering that,if the use of a UIViewController is necessary, should the switch method could be in the scope of the main app delegate?

Some code examples would be great!

  • 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-13T15:11:54+00:00Added an answer on May 13, 2026 at 3:11 pm

    If you want to get it done in code, here is an example I just drummed up using lazy loaded UI elements. I’m only making one button here and swapping it between whichever view is active. It’s slightly awkward, but it reduces the amount of code necessary to demonstrate this.

    I’ve created two UIViews to represent your custom classes, one with a blue background and one with a red. The button swaps between the two. If you have a unique button already in each of your custom views, you just need to either expose those buttons as properties of your UIView subclasses so your view controller can access them, or add the view controller as a target for the button’s action from within your UIView’s loading code.

    I’ve tested this code in my simulator and it seems to work fine, but you really should try to understand what’s going on here so you can implement it yourself.

    ToggleViewController.h:

    #import <UIKit/UIKit.h>
    @interface ToggleViewController : UIViewController {
        UIView *firstView;
        UIView *secondView;
        UIButton *button;
    }
    - (void)addButton;
    - (void)toggleViews;
    @property (nonatomic, readonly) UIView* firstView;
    @property (nonatomic, readonly) UIView* secondView;
    @property (nonatomic, readonly) UIButton* button;
    @end
    

    ToggleViewController.m:

    #import "ToggleViewController.h"
    @implementation ToggleViewController
    
    // assign view to view controller
    - (void)loadView {
        self.view = self.firstView;
    }
    
    // make sure button is added when view is shown
    - (void)viewWillAppear:(BOOL)animated {
        [self addButton];
    
    }
    
    // add the button to the center of the view
    - (void)addButton {
        [self.view addSubview:self.button];
        button.frame = CGRectMake(0,0,150,44);
        button.center = self.view.center;
    }
    
    // to toggle views, remove button from old view, swap views, then add button again
    - (void)toggleViews {
        [self.button removeFromSuperview];
        self.view = (self.view == self.firstView) ? self.secondView : self.firstView;
        [self addButton];
    }
    
    // generate first view on access
    - (UIView *)firstView {
        if (firstView == nil) {
            firstView = [[UIView alloc] initWithFrame:CGRectZero];
            firstView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            firstView.backgroundColor = [UIColor redColor];
        }
        return firstView;
    }
    
    // generate second view on access
    - (UIView *)secondView {
        if (secondView == nil) {
            secondView = [[UIView alloc] initWithFrame:CGRectZero];
            secondView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            secondView.backgroundColor = [UIColor blueColor];
        }
        return secondView;
    }
    
    // generate button on access
    - (UIButton *)button {
        if (button == nil) {
    
            // create button
            button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    
            // set title
            [button setTitle:@"Toggle Views" 
                    forState:UIControlStateNormal];
    
            // set self as a target for the "touch up inside" event of the button
            [button addTarget:self 
                       action:@selector(toggleViews) 
             forControlEvents:UIControlEventTouchUpInside];
        }
        return button;
    }
    
    // clean up
    - (void)dealloc {
        [button release];
        [secondView release];
        [firstView release];
        [super dealloc];
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can you diff two pipelines without using temporary files in Bash? Say you
how to handle button onclick action in iPhone without using interface builder?
I am creating an iOS app without using Interface Builder and I seem to
I need to create IBOutlet and IBActions without using Interface Builder. How can I
Without using Javascript, is there a way to make a CSS property toggle on
I am using ASP.NET ajax to dynamically add/remove controls from a page without using
Is there any way to check whether a file is locked without using a
Can I configure, create/update the existing project in Hudson without using its user interface?
The window's contentView has NSTextFields. (MacOS) i'm using interface builder to setup the window
I am using Autofac 2.4.5.724 under vb.net 2010. I have several classes without default

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.