Reputation: 391
I am creating UIPickerView
programatically
and added a custom button
, when the user pressed the button
, the picker
will not display. But when I pressed the button
, it didn't even call its selector
method
. I know this question have many answers on this website, but it didn't solved my 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];
}
Upvotes: 2
Views: 2005
Reputation: 780
Add toolbar as inputAccessoryView to the text field,
// Tool bar
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,35)];
pickerToolbar.barStyle = UIBarStyleDefault;
// Bar button
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneButtonTapped:)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[pickerToolbar setItems:@[flexSpace,doneButton]];
self.selectedLockerTextFiled.inputAccessoryView = pickerToolbar;
self.selectedLockerTextFiled.inputView = picker;
Upvotes: 0
Reputation: 82759
for example I add this
Step-1
Create the One UIPicker
,UItextField
and NSArray for load the details
@interface ViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
{
UITextField *myTextField;
UIPickerView *myPickerView;
NSArray *pickerArray;
}
@end
Step-2
on your ViewDidLoad
call the picker method like
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self addPickerView];
}
Step-3
create the picker, textfield and 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 a 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 the Done Button resign the 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 I attached the sample project
Sample Output like
Update
if you are used the button hidden the pickerview use this
-(void)donePressed:(id)sender
{
[self.myPickerView removeFromSuperview];
}
when you pressed the button call this method
[self funtion];
Upvotes: 3
Reputation: 9246
Add done button like this:-
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 0, 40, 50);
[doneButton addTarget:self action:@selector(donePressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithCustomView:logoButton];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[self.myPickerView addSubview:toolBar];
Upvotes: 0