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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:43:14+00:00 2026-05-11T19:43:14+00:00

Example: I have 10 view controllers, which are all allocated and initialized in the

  • 0

Example:

I have 10 view controllers, which are all allocated and initialized in the same way:

UIViewController *controller = [[MyViewController alloc] initWithNib];

(note that -initWithNib is a custom method of a UIViewController subclass)

The next view controller class is OtherViewController, and so on. I want to load the view controllers lazily, just when I need them. But to do that, I need to have some kind of “array” that will give me the corresponding class for a given index, so that I can initialize it.

I ended up creating a method with a big switch-statement, that will just do that nasty allocation and initialization separately for every single view controller. I’m not happy with that. There it would be much better if I could assign the appropriate class to a variable, and then at the end of the switch statement just allocate and initialize that class from the variable.

Is there a way to achieve that?

EDIT: I’ve found a function

id class_createInstance(Class cls, size_t extraBytes)

and every class seems to have a property “class”. But I can’t assign it to an instance variable. This doesn’t work:

Class cls = [UIImage class];
cls *image = [cls imageNamed:@"avatar.png"];

The first line compiles. But the second one gives an error: “image undeclared”.

  • 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-11T19:43:15+00:00Added an answer on May 11, 2026 at 7:43 pm

    If you know the names of the classes at compile time, you can assign the classes to Class variables. For example:

    static Class factory[2];
    
    factory[0] = [MyViewController1 class];
    factory[1] = [MyViewController2 class];
    ...
    

    Then you could have (classid would be a constant known at compile time that would map to a desired class:

    -(UIViewController*)createViewController:(int)classid
    {
        return [[factory[classid] alloc] init];
    }
    

    Assuming that method is defined in a class named MyFactory, you can then do:

    MyFactory * fac = [[MyFactory alloc] init];
    UIViewController * v1 = [fac createViewController: 0]; // typed
    id v2 = [fac createViewController: 1]; // untyped
    

    If you don’t have the compile time name of the class, you can simply do the following:

    #include <objc/objc-runtime.h>
    
    id object = [[NSClassFromString(@"TheClassName") alloc] init];
    

    Since your original question involves a set of UIViewControllers though, there’s no reason to lose type safety with the latter method.

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

Sidebar

Ask A Question

Stats

  • Questions 213k
  • Answers 213k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You answered your own question. Yes, store results in @ps,… May 12, 2026 at 10:39 pm
  • Editorial Team
    Editorial Team added an answer I think that usually it would be easier to manage… May 12, 2026 at 10:39 pm
  • Editorial Team
    Editorial Team added an answer Oh you just KNOW you have to go for the… May 12, 2026 at 10:39 pm

Related Questions

The question, in brief: In MVC, how do you distinguish between a checkbox click
I'm working on creating a simple MCV application, in order to understand MVC better.
I'm developing a PHP website that uses url routing. I'd like the site to
OK, so I'm really new to Ruby on Rails. I'm using InstantRails, if that

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.