objective c - Creating a Button to Make Buttons -
i have setup create ui button when button clicked. seemed straightforward @ first, until decided create own ui button class. of works, except fact button not show on screen. have few ideas on might causing it, helpful have pair of eyes @ code.
custombutton.m
- (id)initwithframe:(cgrect)frame //this function provided { self = [super initwithframe:frame]; if (self) { //this stuff wrote, wrong. uibutton* button = [uibutton buttonwithtype:uibuttontyperoundedrect]; button.frame = cgrectmake(1,2,100,50); [button settitle:@"test!" forstate:uicontrolstatenormal]; } return self; nslog(@"worked?"); //this log outputted! however, if put before "return self", doesn't work. i'm guessing problem "return self" not being caught. }
view.m
-(ibaction)createdrawer:(id)sender { custombutton* newbutton = [custombutton alloc]; [newbutton initwithframe:newbutton.frame]; [self.containerview addsubview:newbutton]; nslog(@"asdasdadads"); //this gets executed, when button clicked }
line 3 of view.m gives warning "expression result unused." i'm guessing has "return self" not being used initwithframe?
yes, does, , because of class clusters, should really assigning return value of init
instance:
custombutton *newbutton = [[custombutton alloc] initwithframe:f];
also, conventionally class names begin capital letter. name class custombutton
.
edit: problem in initwithframe:
method, don't set properties on self
on instance (button
). obviously, won't affect state of self
.
Comments
Post a Comment