1

Working off of other Stack Overflow answers, I have managed to come up with the code below:

(in my homescreen.m file)

 UIButton *GCButton;

 -(id)initWithSize:(CGSize)size {
     if (self = [super initWithSize:size]) {
         //add button
         [self addGameCenterButton];      
     }
     return self;
 }

 -(void)addGameCenterButton
 {
     GCButton = [[UIButton alloc]initWithFrame:CGRectMake(50, 20, 30, 30)];
     [GCButton setBackgroundColor:[UIColor orangeColor]];
     [GCButton setTitle: @"My Button" forState:UIControlStateNormal];
     [GCButton setTitleColor: [UIColor blueColor] forState:UIControlStateNormal];
     [GCButton.layer setBorderWidth:1.0f];
     [GCButton.layer setBorderColor:[UIColor blueColor].CGColor];
     //adding action programatically
     [GCButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
     [self.view addSubview:GCButton];   
 }

 -(void)buttonClicked:(UIButton*)sender
 {
     NSLog(@"you clicked on button %ld", (long)sender.tag);
 }

As of right now, I see nothing on my screen. Is there anything else I need to add in my .m or .h files to make the button work?

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
Sooc
  • 141
  • 7

1 Answers1

1

From the comments of your question, I'm assuming you are using SpriteKit. In a GameScene, unfortunately, you cannot use UIButtons normally.

To create buttons for your scene, please check the answers in the link below: Setting up buttons in SKScene

Community
  • 1
  • 1
Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49