Gold Challenge: Button won't appear


So, I am working on the Gold Challenge and decided to add the new button directly rather than adding it to the XIB file, but I cannot get the button to appear on my view. I am sure it is something simple that I am missing.

Here is my viewDidLoad method:

  • (void)viewDidLoad
    [super viewDidLoad] ;

    // get our dimensions to place the button properly
    CGRect bounds = [[self view] bounds] ;

    // create our button
    button = [[UIButton alloc] init] ;
    [button setTitle:@“Move” forState:(UIControlStateNormal)] ;
    [button setCenter:CGPointMake(bounds.size.width - 90, bounds.size.height -90)] ;

    // add button to our view?
    [[self view] addSubview:button] ;

“button” is declared in HeavyViewController.h

@interface HeavyViewController : UIViewController
IBOutlet UIButton *button ;

Thanks in advance…



Dan, I think you need to call initWithFrame for initializing your button, if memory serves.

CGRect buttonFrame = CGRectMake (0, 0, 50, 25);
button = [[UIButton alloc] initWithFrame:buttonFrame];

I had the same problem a while back.

Hope this helps.


Here’s what I did.

[code]- (void)viewDidLoad
movingButton = [[UIButton alloc] initWithFrame:CGRectMake(20, [UIScreen mainScreen].bounds.size.height / 2 , 50, 50)];
[movingButton setTitle:@“Button” forState:UIControlStateNormal];
[movingButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[movingButton setBackgroundColor:[UIColor blackColor]];
movingButton.titleLabel.font = [UIFont systemFontOfSize:10];

[self.view addSubview:movingButton];


  • (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)x duration:(NSTimeInterval)duration
    if (UIInterfaceOrientationIsLandscape(x)) {
    [movingButton setCenter:CGPointMake(self.view.bounds.size.width - 45, self.view.bounds.size.height/2)];
    } else {
    [movingButton setCenter:CGPointMake(45, self.view.bounds.size.height/2)];

When you call initWithFrame, you set the initial origin, so you don’t need to call setCenter:.
And also, you aren’t using Interface Builder, so IBOutlet prefix is superfluous.