ios - UIPickerview done button not working, even its not calling selector method -


i creating uipickerview programatically , added custom button, when user pressed button, picker not display. when pressed button, didn't call selector method. know question have many answers on website, didn't solved problem.

-(void) funtion { self.mypickerview = [[uipickerview alloc]init]; self.mypickerview.datasource = self; self.mypickerview.delegate = self; self.mypickerview.showsselectionindicator = yes; self.mypickerview.frame = cgrectmake(0, 417, 320, 151); [self.mypickerview setbackgroundcolor:[uicolor whitecolor]];  uitoolbar *toolbar= [[uitoolbar alloc] initwithframe:cgrectmake(0,0,320,44)]; [toolbar setbarstyle:uibarstyleblackopaque]; uibarbuttonitem *barbuttondone = [[uibarbuttonitem alloc] initwithtitle:@"done"                                                                   style:uibarbuttonitemstylebordered                                                                  target:self                                                                  action:@selector(donepressed:)]; toolbar.items = [[nsarray alloc] initwithobjects:barbuttondone,nil]; barbuttondone.tintcolor=[uicolor blackcolor];   [self.mypickerview addsubview:toolbar];  [self.view addsubview:self.mypickerview];  }  -(void)donepressed:(id)sender {       [self.mypickerview endediting:yes]; } 

for example add

step-1

create 1 uipicker,uitextfield , nsarray load details

@interface viewcontroller ()<uitextfielddelegate,uipickerviewdatasource,uipickerviewdelegate> {  uitextfield *mytextfield; uipickerview *mypickerview; nsarray *pickerarray; }  @end 

step-2

on viewdidload call picker method like

- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. [self addpickerview]; } 

step-3

create picker, textfield , done button  -(void)addpickerview{ pickerarray = [[nsarray alloc]initwithobjects:@"chess",                @"cricket",@"football",@"tennis",@"volleyball", nil]; mytextfield = [[uitextfield alloc]initwithframe:                cgrectmake(10, 100, 300, 30)]; mytextfield.borderstyle = uitextborderstyleroundedrect; mytextfield.textalignment = nstextalignmentcenter; mytextfield.delegate = self; [self.view addsubview:mytextfield]; [mytextfield setplaceholder:@"pick sport"]; mypickerview = [[uipickerview alloc]init]; mypickerview.datasource = self; mypickerview.delegate = self; mypickerview.showsselectionindicator = yes; uibarbuttonitem *donebutton = [[uibarbuttonitem alloc]                                initwithtitle:@"done" style:uibarbuttonitemstyledone                                target:self action:@selector(done:)]; uitoolbar *toolbar = [[uitoolbar alloc]initwithframe:                       cgrectmake(0, self.view.frame.size.height-                                  mypickerview.frame.size.height-50, 320, 50)]; [toolbar setbarstyle:uibarstyleblackopaque]; nsarray *toolbaritems = [nsarray arraywithobjects:                          donebutton, nil]; [toolbar setitems:toolbaritems]; mytextfield.inputview = mypickerview; mytextfield.inputaccessoryview = toolbar;  } 

step-4

if user press done button resign picker

-(void)done:(id)sender {  [mytextfield resignfirstresponder]; } 

step-5

pickerview delegate methods

#pragma mark - picker view data source -(nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview{ return 1; } -(nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component{ return [pickerarray count]; }  #pragma mark- picker view delegate  -(void)pickerview:(uipickerview *)pickerview didselectrow: (nsinteger)row incomponent:(nsinteger)component{ [mytextfield settext:[pickerarray objectatindex:row]]; } - (nsstring *)pickerview:(uipickerview *)pickerview titleforrow: (nsinteger)row forcomponent:(nsinteger)component{ return [pickerarray objectatindex:row]; } 

here attached sample project

sample output like

enter image description here

update

if used button hidden pickerview use

-(void)donepressed:(id)sender {   [self.mypickerview removefromsuperview];  } 

when pressed button call method

[self funtion]; 

Comments