ios - UIPickerView doesn't appear -
in viewcontroller call push, try programmatically display combobox. combobox implement uipickerview delegate protocol , add .xib file.
when run app, can see combobox on screen, when click on it, nothing append. pickerview displayed.
what don't understand, in viewcontroller call modal works fine
// // combobox.h // #import <uikit/uikit.h> @interface combobox : uiviewcontroller<uipickerviewdelegate, uipickerviewdatasource, uitextfielddelegate> { uipickerview* pickerview; iboutlet uitextfield* textfield; nsmutablearray *dataarray; } -(void) setcombodata:(nsmutablearray*) data; //set picker view items -(void) setplaceholder:(nsstring*) label; @property (retain, nonatomic) nsstring* selectedtext; //the uitextfield text @property (retain, nonatomic) iboutlet uitextfield* textfield; //the uitextfield @end // // combobox.m // #import "combobox.h" @implementation combobox @synthesize selectedtext; @synthesize textfield; - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { return [super initwithnibname:nibnameornil bundle:nibbundleornil]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; } #pragma mark - view lifecycle - (void)viewdidload { [super viewdidload]; } - (void)viewdidunload { [super viewdidunload]; } - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { return (interfaceorientation == uiinterfaceorientationportrait); } //-- uipickerviewdelegate, uipickerviewdatasource - (nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview; { return 1; } - (void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component { textfield.text = [dataarray objectatindex:row]; selectedtext = textfield.text; } - (nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component; { return [dataarray count]; } - (nsstring *)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component; { return [dataarray objectatindex:row]; } //-- combobox -(void) setcombodata:(nsmutablearray*) data { dataarray = data; } -(void) setplaceholder:(nsstring *)label { textfield.placeholder = label; } -(void)doneclicked:(id) sender { [textfield resignfirstresponder]; //hides pickerview } - (ibaction)showpicker:(id)sender { pickerview = [[uipickerview alloc] init]; pickerview.showsselectionindicator = yes; pickerview.datasource = self; pickerview.delegate = self; uitoolbar* toolbar = [[uitoolbar alloc] init]; toolbar.barstyle = uibarstyleblacktranslucent; [toolbar sizetofit]; //to make done button aligned right uibarbuttonitem *flexiblespaceleft = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace target:nil action:nil]; uibarbuttonitem* donebutton = [[uibarbuttonitem alloc] initwithtitle:@"done" style:uibarbuttonitemstyledone target:self action:@selector(doneclicked:)]; [toolbar setitems:[nsarray arraywithobjects:flexiblespaceleft, donebutton, nil]]; //custom input view textfield.inputview = pickerview; textfield.inputaccessoryview = toolbar; } - (bool)textfieldshouldbeginediting:(uitextfield *)atextfield { [self showpicker:atextfield]; return yes; } @end
the viewdidload of viewcontroller
- (void)viewdidload { [super viewdidload]; nsmutablearray* serveurarray = [[nsmutablearray alloc] init]; [serveurarray addobject:@"1"]; [serveurarray addobject:@"2"]; [serveurarray addobject:@"3"]; comboserveur = [[combobox alloc] init]; [comboserveur setcombodata:serveurarray]; //assign array combobox comboserveur.view.frame = cgrectmake(95, 220, 130, 31); //combobox location , size (x,y,width,height) [self.view addsubview:comboserveur.view]; }
thx answers
i assume targeting ios 5.0 , above. since adding view of viewcontroller viewcontroller can use childviewcontroller introduced in ios 5.0.
modify viewdidload method
- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. combobox *comboserveur = [[combobox alloc]initwithnibname:@"comboboxcontroller" bundle:nil]; [comboserveur setcombodata:@[@"1",@"2",@"3"]]; comboserveur.view.frame = cgrectmake(50.0f, 200.0f, 220.0f, 31.0f); [self.view addsubview:comboserveur.view]; [self addchildviewcontroller:comboserveur]; [comboserveur didmovetoparentviewcontroller:self]; }
few steps check
- make view of
combobox
viewcontroller freeform masktypeuiviewautoresizingnone
. - check textfield , delegate of textfield connected
Comments
Post a Comment