Button's title label not appearing?

Discussion in 'iOS Programming' started by ArtOfWarfare, Sep 4, 2011.

  1. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    I have this code:
    Code:
    NSString *path = [[NSBundle mainBundle] pathForResource:category ofType:@"txt"];
        NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF16BigEndianStringEncoding error:NULL];
        characters = [string componentsSeparatedByString:@"\n"];
        
        [characters enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop)
         {
             UIButton *key = [UIButton buttonWithType:UIButtonTypeRoundedRect];
             key.frame = CGRectMake((index%6)*(self.frame.size.width/6.0),
                                    (floor(index/6.0))*(self.frame.size.width/6.0),
                                    self.frame.size.width/6.0,
                                    self.frame.size.width/6.0);
             key.titleLabel.text = obj;
             key.titleLabel.textColor = [UIColor blackColor];
             key.titleLabel.font = [UIFont systemFontOfSize:5.0];
             key.titleLabel.frame = CGRectMake(10, 10, 50, 50);
             key.titleLabel.backgroundColor = [UIColor redColor];
             [self addSubview:key];
         }];
    What it does is it finds a txt file full of strings. I want all the strings to become titles for a grid of buttons.

    I know it's accessing the strings fine, because I had an NSLog mixed in spitting out the strings that it was having as a label for each button. The grid of buttons is also displaying fine.

    But the titleLabel is in no way showing up. The red background doesn't show up. The black text doesn't show up. I've tweaked the font size and the titleLabel.frame (including not touching the properties and leaving it as default) a lot but the results are all the same: nothing shows up.

    Is there something I'm doing wrong?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    UIButton's titleLabel is not set via the property but instead via the setTitle:forState: instance method.
     
  3. ArtOfWarfare thread starter macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    Thanks. Seems like I end up having to be told this once a year at least...
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    No worries. If you only need to be reminded once a year and not once a week, I think you'll be okay. :D
     

Share This Page