objective c - Can not call class method -
i'm building tag-based application , want call same function each tab (viewcontroller
).
i'm trying in following way:
#import "optionsmenu.h" - (ibaction) optionsbutton:(id)sender{ uiview *optionsview = [options showoptions:4]; nslog(@"options view tag %d", optionsview.tag); }
optionsmenu.h
file:
#import <uikit/uikit.h> @interface optionsmenu : uiview - (uiview*) showoptions: (nsinteger) tabnumber; @end
optionsmenu.m
file:
@import "optionsmenu.h" @implementation optionsmenu - (uiview*) showoptions:(nsinteger) tabnumber{ nslog(@"show options called"); uiview* optionsview = [[uiview alloc] initwithframe:[[uiscreen mainscreen] bounds]]; optionsview.opaque = no; optionsview.backgroundcolor = [[uicolor blackcolor] colorwithalphacomponent:0.5f]; //creating several buttons on optionsview optionsview.tag = 100; return optionsview; } @end
the result never "show options called" debug message , optionsview.tag
0
.
what doing wrong?
i understand easy , stupid question, not able solve myself.
any feedback appreciated.
first thing note instance method (and not class method described in question title). means in order call method should have alloc/init instance of class , send message instance. example:
// note here class names (by convention) begin // uppercase letter, optionsmenu should preffered optionsmenu *options = [[optionsmenu alloc] init]; uiview *optionsview = [options showoptions:4];
now, if want create class method returns preconfigured uiview
, try (provided not need access ivars in method):
// in header file + (uiview *)showoptions:(nsinteger)tabnumber; // in implementation file + (uiview *)showoptions:(nsinteger)tabnumber{ nslog(@"show options called"); uiview *optionsview = [[uiview alloc] initwithframe:[[uiscreen mainscreen] bounds]]; optionsview.opaque = no; optionsview.backgroundcolor = [[uicolor blackcolor] colorwithalphacomponent:0.5f]; //creating several buttons on optionsview optionsview.tag = 100; return optionsview; }
and send message this:
uiview *optionsview = [optionsmenu showoptions:4]; //sending message class here
finally not forget of course add view subview in order display it. hope makes sense...