ios - Creating multiple Custom Views from XIB and adding them programmatically into ViewController -
i start off describing i'm trying accomplish , follow describing i've tried already. pretty sure i've tried not best approach please correct approach needed!
i have viewcontroller.m , custom view laid out in customview.xib. custom view has uibuttons , uilabels populated array of custom objects.
the user flow should go such: viewcontroller starts off showing customview labels populated
customobjectarray[0] -> user presses button -> "copy" of customview slides view, on previous version.
it's labels , buttons populated customobjectarray[1] -> user presses button -> repeat until end of array.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
so far made viewcontroller.m/h, customview.m/h , customview.xib file. used interface builder layout.
on "custom class" tab top-level view in xib file, type in "customview". drag iboutlets xib file customview header (.h) file.
in viewcontroller, under -(instancetype) init method, create custom view using normal initwithnib method. , do:
self.view = customviewvariablename; when run program, view show's fine. however, when try selector's, nothing's getting recognized buttons:
[currentcustomview.continuebutton addtarget:self action:@selector(continuebuttonpressed:) forcontrolevents:uicontroleventtouchupinside]; what did wrong here?
more importantly, given described goals top, doing right? need have customview.m/h files? or can same thing xib , viewcontroller file. remember need have "multiple copies" , slide them on top of each other until end of custom objects array.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ more code requested.
in viewcontroller:
- (instancetype)init { currentcustomview = [[[nsbundle mainbundle] loadnibnamed:@"customview" owner:self options:nil] objectatindex:0]; self.view = currentquizquestionview; ....... return self; } - (void)viewdidload { [super viewdidload]; // additional setup after loading view nib. [currentcustomview.continuebutton addtarget:self action:@selector(continuepressed:) forcontrolevents:uicontroleventtouchupinside]; } -(void)continuepressed:(id)sender{ nslog(@"current position"); //[self moveinquestion]; } in customview.h:
#import <uikit/uikit.h> @interface customview : uiview @property (weak, nonatomic) iboutlet uibutton *continuebutton; @end in customview.m:
this default page, added nothing in file.
customview.xib:
i'm not sure if understand requirements. let's assume have several customviews subviews of viewcontroller , subviews can display each each after pressing own button.
first thing adding subview:
self.view = customviewvariablename; since customsviews subviews according assumption, above line error. have this.
for (nsinteger = 0; < 5; i++) { customview *v = [[customview alloc] initwithnib]; [v.button addtarget:self action:@selector(continuepressed:) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:v]; } second, noticed have array handler refer subviews. can add above customview mutable array insert 1 more line above for-loop.
[_customobjectarray addobject:v.button]; finally, created simple project , tried implement things mentioned. maybe can take reference. https://db.tt/och2tzyg


Comments
Post a Comment