12

In a UIViewController's viewDidLoad method, I do this:

UIButton *b = [[UIButton buttonWithType:UIButtonTypeRoundedRect] 
                                       initWithFrame:CGRectMake(0, 0, 100, 100)];

[b setTitle:@"Testing" forState:UIControlStateNormal];
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
[self.view addSubview:b];                       // EDIT: should be 'b'
NSLog(@"button title: %@", [b titleLabel].text);

The button displays but the title doesn't. The NSLog line prints "Testing" to the console. Any suggestions on what I'm doing wrong?

2
  • Two ideas: * What happens if you make your button larger? * Are you doing anything else in viewDidLoad that could be affecting the button?
    – JonathonW
    Jun 26, 2009 at 4:48
  • Please see diederikh's solution below as it describes an error in the double initializer used above as well as the fix for it, which is to assign the frame separately, after the convenience initializer [... button with type ...] completes. The rest of the OP's own solution is correct and remains the same.
    – eGanges
    Jun 21, 2015 at 3:46

6 Answers 6

38

I cannot tell you why it does not work, but I do have a solution:

UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;        
b. frame = CGRectMake(0, 0, 100, 100);

[b setTitle:@"Testing" forState:UIControlStateNormal];
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
[self addSubview:b];   

Seperate creating the frame from the allocation and init of the button.

2
  • I agree. I bet chaining two initializers together like you're doing is causing some issues. Use the buttonWithType then set the frame.
    – CIFilter
    Jun 26, 2009 at 4:51
  • Yeah, buttonWithType is a static factory method that returns a fully initialized button. You can't call initWithFrame on an already-initialized button (the result is at best undefined). init methods are only intended to be called after alloc.
    – devios1
    Jan 22, 2016 at 21:18
22

The problem lies with

UIButton *b = [[UIButton buttonWithType:UIButtonTypeRoundedRect] 
                                       initWithFrame:CGRectMake(0, 0, 100, 100)];

buttonWithType returns an autoreleased initialized object. You cannot send it an initWithFrame again as an object can only be initialized once.

Set its frame separately:

b.frame = CGRectMake(0, 0, 100, 100);
4

Do this instead:

UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(0, 0, 100, 100);
[b setTitle:@"Testing" forState:UIControlStateNormal];
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
[self.view addSubview:b];  
0
0

The thing is you have to go for [[UIButton alloc] initWithFrame:] or [UIButton buttonWithType:], you should not use both together.

0

You can do the following:

UIButton *b=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

b=[UIButton buttonWithType:UIButtonTypeRoundedRect];

[b setTitle:@"Button Title Here" forState:UIControlStateNormal];
[b setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
[self.view addSubview:b];
0

I had the same problem and it ended up being the fact that I was setting the image rather than the backgroundImage. It all worked fine when I did:

[button setBackgroundImage:image forState:UIControlStateNormal];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.