ios - How do I properly add a button to my scene using only code? -
working off of other stack overflow answers have managed come code below.
(in 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 see nothing on screen. else need add in .m or .h files make button work?
thanks in advanced help
from comments of question, i'm assuming using spritekit
. in gamescene
, unfortunately, cannot use uibuttons
normally.
to create buttons scene, please check answers in link below: setting buttons in skscene
Comments
Post a Comment