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:-)

Community
  • 1
  • 1
jwknz
  • 6,598
  • 16
  • 72
  • 115
  • Why do you alloc your UIView in the separate void method? Just put the code in the separate void method the exact way it is in your view did load. – SimplyKiwi May 25 '12 at 01:00
  • Cool, because I didn't know I could:-) Would you like to post it as an answer so i can accept it for you?? cheers iBrad Apps – jwknz May 25 '12 at 01:03
  • Also change backgroundView to self.view if you didn't already do that – SimplyKiwi May 25 '12 at 01:05

3 Answers3

2

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.

WendiKidd
  • 4,333
  • 4
  • 33
  • 50
  • Glad to help! To accept you just click the check mark to the left of the answer--you shouldn't have to wait to do so, it should work any time :) – WendiKidd May 25 '12 at 01:08
  • I have to wait 7 minutes after I asked the question:-) I get a warning pop up otherwise:-) I know:-) tried it several times. – jwknz May 25 '12 at 01:40
  • 1
    Huh! That's odd, I didn't know that. See, you taught me something today too! Thanks for accepting and I hope your continued forays into iPhone development go well! – WendiKidd May 25 '12 at 01:51
0

Your backgroundColor method in the second instance is just setting the background colour on some different UIView object that gets created and then leaked. (The method name is also not great -- its suggests a property getter, but doesn't actually get a property or return anything.)

Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
0

The problem here is you method backgroundColour isn't declared before it's used. So there are 3 ways to get around this

  1. Move your backgroundColor method above viewDidLoad
  2. Declare the backgroundColour as a public method in the @interface
  3. Create a private interface and declare backgroundColour

No.3 Example:

@interace MyViewController (private) - (void)backgroundColor; @end

Note if your interface is inherits from anything still write the private interface as above.

Also don't create a new view just perform [self.view setBackgroundColor:[UIColor redColor]];

CStreel
  • 2,642
  • 2
  • 19
  • 37