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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:03:09+00:00 2026-06-04T15:03:09+00:00

. Hi, I am wanting to clean up my viewDidLoad method as I it

  • 0

.

Hi,

I am wanting to clean up my viewDidLoad method as I it is growing in number of lines of code and it is getting messy.

Now my UI is build programmatically, because I want to learn that way of doing things.

So I read on this SO Post that I can set the UI items in a seperate -9void) method and then link to that void by using [self method]

Now when I use that way, it does not seem to work for me.

like, if I want to set the back ground color this will work:

- (void)viewDidLoad
 {
[super viewDidLoad];

self.view.backgroundColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.38 alpha:1.0];
}

but this does not:

- (void)viewDidLoad
{
[super viewDidLoad];

[self backgroundColor];


// Do any additional setup after loading the view.
}

-(void)backgroundColor
{
UIView *backgroundView = [[UIView alloc] init]; 

backgroundView.backgroundColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.38  alpha:1.0];
}

Am I misunderstanding this??

Thanks in Advance:-)

  • 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-06-04T15:03:10+00:00Added an answer on June 4, 2026 at 3:03 pm

    I think you’re misunderstanding the nature of views/view controllers.

    In this line:

    self.view.backgroundColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.38 alpha:1.0];
    

    self refers to the view controller you are loading, and view refers to the view that is owned by that view controller. So when you set self.view.backgroundColor, you are setting the background color for the view that will be presented by the view controller you are displaying. Therefore that works.

    Your other method, on the other hand, doesn’t do that:

    UIView *backgroundView = [[UIView alloc] init]; 
    

    That line creates an entirely new UIView instance, and sets its background color. This is a brand new view, and is NOT the same view which is referenced earlier by self.view.

    If you really want to have a separate function that changes the background color for you, write it like this:

    -(void) setBackgroundColor
    {
       self.view.backgroundColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.38 alpha:1.0];
    }
    

    Then you’re actually modifying the view that belongs to the view controller, and the change should actually display.


    I would also suggest that this function isn’t very useful; you’re creating a function that encapsulates a single line of code that will never change. There’s really not much point in making a function for that. A more useful implementation might be:

    -(void) setBackgroundColor:(UIColor)newColor
    {
       self.view.backgroundColor = newColor;
    }
    

    To use this method, you would write the following line in your viewDidLoad method:

    [self setBackgroundColor:[UIColor colorWithRed:0.0 green:0.2 blue:0.38 alpha:1.0]];
    

    And then you could call setBackgroundColor again whenever you wanted, to change the background color to different values.

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

Sidebar

Related Questions

I've been wanting to improve my regex skills for quite some time now and
Ok, Wanting to MVVM an existing app. The code behind contains the following: private
This is what my header looks like right now at http://www.bolistylus.com I am wanting
I've been wanting to learn some Haskell for a while now, and I know
so I'm pretty much wanting to make my code cleaner, and all of my
I've been wanting to build a shared library of which will utilize classes to
I'm wanting to get the magic number from a git packfile index to ensure
I have a gross looking function and am wanting to clean it up. All
I have a RichTextBox, and I am wanting to code it, that when the
Wanting to integrate aspects of SharePoint into an existing website - rather than integrating

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.