0

I load some defaults and set UIButton customizations in my viewDidLoad method. The defaults load fine. However, the UIButtons do not get their customization.

When I put the exact same UIButton code in an action button to trigger the customizations - it works great. I changed the button to "Custom" in the .xib.

I feel like something is happening out of order and that is why the buttons do not launch with their mods.

- (void)viewDidLoad {

    lifeTotal.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"bankKey"];
    floatTot = [[NSUserDefaults standardUserDefaults] floatForKey:@"floatKey"];
    dollaInt = [[NSUserDefaults standardUserDefaults] integerForKey:@"intKey"];
    hourlyRate.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"rateKey"];
    rate = [[NSUserDefaults standardUserDefaults] integerForKey:@"rateInt"];

    NSArray *buttons = [NSArray arrayWithObjects: self.start, self.stop, self.fbButton, self.clearLifeTotal,nil];


    for(UIButton *btn in buttons)
    {
        // Set the button Text Color
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

        // Set default backgrond color
        [btn setBackgroundColor:[UIColor blackColor]];

        // Round button corners
        CALayer *btnLayer = [btn layer];
        [btnLayer setMasksToBounds:YES];
        [btnLayer setCornerRadius:5.0f];

        // Apply a 1 pixel, black border around Buy Button
        [btnLayer setBorderWidth:1.0f];
        [btnLayer setBorderColor:[[UIColor blackColor] CGColor]];

    }
    [super viewDidLoad];
}
Kyle Luchinski
  • 153
  • 1
  • 4
  • 18
  • Have you tried putting `[super viewDidLoad];` as the first line in your method? – neilvillareal Feb 14 '13 at 02:59
  • Try putting the code in viewDidAppear instead. – rdelmar Feb 14 '13 at 03:08
  • Thanks for the idea. Still not working! – Kyle Luchinski Feb 14 '13 at 03:13
  • Can you check if your loop is running? I mean control is going inside your loop? – Rushi Feb 14 '13 at 04:03
  • 1
    Setting attributes in viewDidLoad should work fine. Something else is going on. How is the view controller loaded? – railwayparade Feb 14 '13 at 04:51
  • 1
    You can try removing and relinking your UIButtons to the xib? – esh Feb 14 '13 at 05:27
  • This is what I did. Step 1. Added 4 buttons. Step 2. Set style of buttons to Custom. Step 3. Linked Button outlets to View Controller. Step 4. Added your code. Step 5. Execute the code, look at the customized buttons. Step 6. There is no step 6. – esh Feb 14 '13 at 05:36
  • @BlackFlam3 Great Idea. Just tried but still doesnt work. I added code from my .h above if that helps – Kyle Luchinski Feb 15 '13 at 22:56
  • 1
    When you set a breakpoint on the line with `NSArray *buttons = …` and type `po self.start`, `po self.clearLifeTotal`, etc., what are the return values? Also, is your XIB file linking the buttons to the instance variables you've declared or the @properties? – Aaron Brager Feb 15 '13 at 23:00
  • @BlackFlam3 Got it to work! Dragged the line from the (@)property line from the .h to the .xib worked better than from within the .xib.. Doesn't make sense to me but Ill take it! Thanks much – Kyle Luchinski Feb 15 '13 at 23:02
  • It happens sometimes, although I am unable to figure out why, and it invariably leads me to relinking the controls, and they start working. – esh Feb 16 '13 at 08:19

3 Answers3

0

Have you tried putting [super viewDidLoad] at the top of the method, before your code?

Rich Schonthal
  • 472
  • 2
  • 12
0

Try to replace your for loop with this and check :

    for (UIButton *btn in self.view.subviews)
    {
        if (btn == self.start || btn == self.stop || btn == self.fbButton || btn == self.clearLifeTotal)
        {
            // Set the button Text Color
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

            // Set default backgrond color
            [btn setBackgroundColor:[UIColor blackColor]];

            // Round button corners
            CALayer *btnLayer = [btn layer];
            [btnLayer setMasksToBounds:YES];
            [btnLayer setCornerRadius:5.0f];

            // Apply a 1 pixel, black border around Buy Button
            [btnLayer setBorderWidth:1.0f];
            [btnLayer setBorderColor:[[UIColor blackColor] CGColor]];
        }
    }
Rushi
  • 4,553
  • 4
  • 33
  • 46
0

Sorry to say that a quick look at UIButton.h shows that viewDidLoad is not exposed on a UIButton. It is, therefore, not being called.

You're looking for -(id)initWithFrame:(CGRect)frame or -(id)initWithCoder:(NSCoder *)aDecoder.

C Fairweather
  • 696
  • 4
  • 20