How to add done button on uidatepicker? - ios

UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
[txt_time setInputView:datePicker];
UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(addORDeleteRows)];
How to add done button on top of the uidatepicker when I press on textfield then uidatepicker will show after entering the date done button also show. After clicking on done button uidatepicker should be hidden.

Alloc a view add datepicker at bottom to the view and toolbar on the top of datepicker to the view.
Add barbuttons to the toolbar.

//First declare all the property and set the delegate and datasource in .h like this
#interface RegistrationView : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate,UIActionSheetDelegate>
{
UIPickerView *picker;
UIToolbar *toolbarPicker;
UIActionSheet *actionPicker;
}
#property (strong,nonatomic) UIPickerView *picker;
#property (strong,nonatomic) UIToolbar *toolbarPicker;
#property (strong,nonatomic) UIActionSheet *actionPicker;
//and synthesis in .m file
#synthesize picker,actionPicker,toolbarPicker;
// in View did load method put call the setting method for picker
[self pickerSetting];
// set picker,action sheet and toolbar through This method
-(void) pickerSetting
{
toolbarPicker = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,44)];
if (!g_IS_IOS_6)
{
toolbarPicker.barTintColor = [UIColor colorWithRed:72.0/255.0 green:197.0/255.0 blue:87.0/255.0 alpha:1.0];
}
toolbarPicker.frame=CGRectMake(0,0,320,44);
actionPicker = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
actionPicker.frame = CGRectMake(0, 234, 320, 256);
[actionPicker setActionSheetStyle:UIActionSheetStyleDefault];
actionPicker.delegate = self;
UIImage *imgbtnConvert = [UIImage imageNamed:#"info_button.png"];//Done btn.png
UIButton *btnConvert = [UIButton buttonWithType:UIButtonTypeCustom];
btnConvert.bounds = CGRectMake(200, 0, 93,31);
[btnConvert setTitle:#"Done" forState:UIControlStateNormal];
[btnConvert setTintColor:[UIColor whiteColor]];
[btnConvert setBackgroundImage:imgbtnConvert forState:UIControlStateNormal];
[btnConvert addTarget:self action:#selector(pickerDoneClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItemConvert = [[UIBarButtonItem alloc] initWithCustomView:btnConvert];
UIBarButtonItem *flexConvert = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIImage *imageConvert = [UIImage imageNamed:#"info_button.png"];//cancel png
UIButton *buttonConvert = [UIButton buttonWithType:UIButtonTypeCustom];
buttonConvert.bounds = CGRectMake( 0, 0, 93,31);
[buttonConvert setTitle:#"Cancel" forState:UIControlStateNormal]; //set title
[buttonConvert setTintColor:[UIColor whiteColor]]; //font color
[buttonConvert setBackgroundImage:imageConvert forState:UIControlStateNormal]; //set background image
[buttonConvert addTarget:self action:#selector(pickerCancelClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barCancelButtonItemConvert = [[UIBarButtonItem alloc] initWithCustomView:buttonConvert];
NSArray *itemsConvert = [[NSArray alloc] initWithObjects:barCancelButtonItemConvert, flexConvert, barButtonItemConvert, nil];
[toolbarPicker setItems:itemsConvert];
[actionPicker addSubview:toolbarPicker];
picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
picker.backgroundColor= [UIColor whiteColor];
[actionPicker addSubview:picker];
}
// and add two mehtod for picker done button and cancel button click .
-(void) pickerDoneClick
{
// picker done button click put here your code ..
[actionPickerSearchCar dismissWithClickedButtonIndex:0 animated:YES];
}
-(void) pickerCancelClick {
// Cancel button click
[actionPickerSearchCar dismissWithClickedButtonIndex:0 animated:YES];
}
// and put the delegate and data source method
#pragma mark -
#pragma mark UIPickerView Delegate Datasource Method
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// return yourarr.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
// return yourarr[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// selectedfiles =yourarr[row];
}
// call this method for allocation picker .
[self pickerSetting]
// and show picker using this (put This in button action method)
-(IBAction)showPicker:(id)sender
{
[actionPickerSearchCar showInView:self.view];
[actionPickerSearchCar setBounds:CGRectMake(0, 0, 320, 465)];
}
I hope , it's helpful for you . Thank you.

I think you might really want to use the inputAccessoryView property of UITextField. Just put an UIToolbar there with your UIBarButtonItem and use the target action to call [txt_time resignFirstResponder].

Related

UIPicker subview not recognizing input

I am trying to dismiss a UIPickerView using a toolbar similar to the inputAccessoryView on keyboards.
I am using the same UIToolbar for both my text field and my picker, the textfield will dismiss properly, but the picker doesn't even recognize my touch on the button.
This is the code I am using to create the toolbar:
UIToolbar *tipToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
tipToolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *donebtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignResponder:)];
tipToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
donebtn, nil
];
[tipToolbar sizeToFit];
_outlet_txt_ipaddr.inputAccessoryView = tipToolbar;
[_outlet_picker addSubview:tipToolbar];
And this is the action that will dismiss the keyboard or the picker:
- (IBAction)resignResponder:(id)sender{
[_outlet_txt_ipaddr resignFirstResponder];
[_outlet_picker resignFirstResponder];
}
The toolbar appears on the picker view, but my tap on the done button isn't even showing the animation let alone call the resignResonder action.
for picker view [_outlet_picker resignFirstResponder]; will not work.
you need to implement the delegate for picker view.
try this code
UIToolbar *tipToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
tipToolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *donebtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignResponder:)];
tipToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
donebtn, nil
];
[tipToolbar sizeToFit];
_outlet_txt_ipaddr.inputAccessoryView = tipToolbar;
_outlet_picker.delegate=self;
_outlet_picker.dataSource=self;
_outlet_picker.showsSelectionIndicator=YES;
[self.view addSubview:outlet_picker];
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [pickerValueAry count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
NSMutableArray *ary = [[NSMutableArray alloc] initWithArray:pickerValueAry];
id str=[ary objectAtIndex:row];
return str;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"selectedRowInPicker >> %d",row);
}
you can dismiss the picker view on row select.

display done button on UIPickerview

I have written the following code in the viewDidLoad method:
categoryPickerView=[[UIPickerView alloc]init];
categoryPickerView.alpha = 0;
[self.view addSubview:categoryPickerView];
categoryPickerView.delegate=self;
categoryPickerView.tag=1;
and called this method to hide picker view
- (IBAction)hidePickerView:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200);
categoryPickerView.transform = transfrom;
categoryPickerView.alpha = categoryPickerView.alpha * (-1) + 1;
[UIView commitAnimations];
}
My problem is that I want to display a "Done" button on a picker view and the picker view should hide on button click.
You can use this code,
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(changeDateFromLabel:)];
toolBar.items = #[barButtonDone];
barButtonDone.tintColor=[UIColor blackColor];
[pickerView addSubview:toolBar];
//(or)pickerView.inputAccessoryView = toolBar;
and set button action method for changeDateFromLabel:
-(void)changeDateFromLabel:(id)sender
{
[[your UI Element] resignFirstResponder];
}
You can create view and add toolbar with "Done" button and UIPickerView as subviews
- (void)createInputView {
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 44)];
[toolBar setBarStyle:UIBarStyleDefault];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(doneClicked)];
toolBar.items = #[flex, barButtonDone];
barButtonDone.tintColor = [UIColor blackColor];
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, toolBar.frame.size.height, screenWidth, 200)];
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, toolBar.frame.size.height + picker.frame.size.height)];
inputView.backgroundColor = [UIColor clearColor];
[inputView addSubview:picker];
[inputView addSubview:toolBar];
textField.inputView = inputView;
}
- (void)doneClicked {
[textField resignFirstResponder];
}
You need to use the UIToolbar as the accessory view:
Try with this:
#pragma mark - PickerView for Location Selection
- (UIPickerView *)locationsPicker {
if ( locationsPicker == nil ) {
locationsPicker = [[UIPickerView alloc] init];
locationsPicker.delegate = self;
locationsPicker.dataSource = self;
locationsPicker.showsSelectionIndicator = YES;
}
return locationsPicker;
}
- (UIToolbar *)accessoryView {
if ( accessoryView == nil ) {
accessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemDone
target:self
action:#selector(onLocationSelection)];
[accessoryView setItems:[NSArray arrayWithObject:doneButton]];
}
return accessoryView;
}
- (void)onLocationSelection {
NSInteger row = [self.locationsPicker selectedRowInComponent:0];
if ( [Location isFirstResponder] ) {
NSLog(#"%#", [listOfLocations objectAtIndex:row]);
[Location resignFirstResponder];
}
}
Copy-pasteable solution!
This works in iOS 10 (and probably other versions too) I wrote this code on 9/4/2017.
I needed to combine other answers to get what I wanted, which was a Cancel button, Done button, and a picker view that would show/hide on the tap of a button. Ignore the fact that my screenshot only has one item in the picker.. I only have one item in my array. I've tested multiple items, it works fine.
Disclaimer! I have tested and used this code. I generalized the names in my example, so I apologize in advance if I missed one and they property names don't line up.
In order to have a pickerView that appears on the tap of a button, you need to create a dummyTextField (of type UITextField anywhere in your view controller. This is because UIPickerView's are designed to work with UITextFields. To mimic showing and hiding of the picker, your button taps need to call the dummyTextField's becomeFirstResponder and resignFirstResponder methods. (examples are below)
Step one
Create a custom data source class. MyObj is the class you want to have items show up in the picker for.
In .h:
#import <UIKit/UIKit.h>
#class myObj;
#interface PickerDataSource : NSObject <UIPickerViewDataSource, UIPickerViewDelegate>
#property (nonatomic, strong) MyObj *selectedObject;
#end
In .m:
#import "PickerDataSource.h"
#import "MyObj.h"
#implementation PickerDataSource
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.selectedObject = mySourceArray[row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return mySourceArray.count;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return ((MyObj *)mySourceArray[row]).myTitle;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
#end
Step two In your view controller, import custom data source, delegate, and set properties: (You must include the text field delegate!)
#import "PickerDataSource.h"
#interface MyViewController () <UIPickerViewDelegate, UITextFieldDelegate>
#property (strong, nonatomic) PickerDataSource *pickerDataSource;
#property (strong, nonatomic) UIPickerView *picker;
#property (strong, nonatomic) UITextField *dummyTextField;
#end
Step three
In your viewDidLoad, call [self setupPicker]; (you'll create this method next)
Step four
Create setupPicker
- (void)setupPicker {
// init custom picker data source
self.pickerDataSource = [PickerDataSource new];
// init custom picker
self.picker = [UIPickerView new];
// set the picker's dataSource and delegate to be your custom data source
self.picker.dataSource = self.pickerDataSource;
self.picker.delegate = self.pickerDataSource;
self.picker.showsSelectionIndicator = YES;
// next step is to write this configure method getting called here
[self configurePickerSubviews];
// lastly, add the dummyTextField to your view.
[self.view addSubview:self.dummyTextField];
}
Step five
Create configurePickerSubviews
- (void)configurePickerSubviews {
// A UIPickerView must be added as an inputView to a UITextField in order to be displayed on button tap
// So you need to create a dummyTextField to do so.
self.dummyTextField = [UITextField new];
// Create a toolbar to add a done button
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,44)];
[toolBar setBarStyle:UIBarStyleDefault];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(locationPressed)];
// Create a flex space so that done button will be right aligned
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(dismissPicker)];
toolBar.items = #[cancel, flex, done];
done.tintColor = [UIColor blackColor];
[self.picker addSubview:toolBar];
// Create an input view to add picker + done button as subviews
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.picker.frame.size.height + 44)];
[self.picker setFrame:CGRectMake(0, 0, inputView.frame.size.width, inputView.frame.size.height)];
inputView.backgroundColor = [UIColor clearColor];
[inputView addSubview:self.picker];
[inputView addSubview:toolBar];
// Set custom inputView as container for picker view
self.dummyTextField.inputView = inputView;
// Hiding the textfield will hide the picker
[self.dummyTextField setHidden:YES];
}
Step six
Configure the PickerDataSource so that it is fetching the data you want to display from your source array.
Step 7
Click Run!
The UIToolbar with the 'Done' button should be added to the inputAccessoryView of the view that becomes first responder. As the UIView class inherits from UIResponder, any view can potentially contain an inputView and inputAccessoryView. So instead of manually performing the animations programmatically, you could use the default animation behaviour that comes with the UIResponder's keyboard show/hide animation.
Subclass a UIView and override the inputView and inputAccessoryView properties and make them readwrite. In this example, I will subclass a UITableViewCell.
// FirstResponderTableViewCell.h
#interface FirstResponderTableViewCell : UITableViewCell
#property (readwrite, strong, nonatomic) UIView *inputView;
#property (readwrite, strong, nonatomic) UIView *inputAccessoryView;
#end
Override canBecomeFirstResponder in your subclass' implementation.
// FirstResponderTableViewCell.m
- (BOOL)canBecomeFirstResponder {
return YES;
}
In your view controller, create and assign the picker view and input accessory toolbar
// MyViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *pickerView = [[UIPickerView alloc] init];
UIToolbar *accessoryToolbar = [UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
// Configure toolbar .....
// note: myFirstResponderTableViewCell is an IBOutlet to a static cell in storyboard of type FirstResponderTableViewCell
self.myFirstResponderTableViewCell.inputView = pickerView;
self.myFirstResponderTableViewCell.inputAccessoryView = accessoryToolbar;
}
Don't forget to assign first responder to the view when required (e.g. inside - tableView:didSelectRowAtIndexPath:)
[self.myFirstResponderTableViewCell becomeFirstResponder];
Hope this helps.
Reference: http://blog.swierczynski.net/2010/12/how-to-create-uipickerview-with-toolbar-above-it-in-ios/
Try this.
UIPickerView *cityPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
cityPicker.delegate = self;
cityPicker.dataSource = self;
[cityPicker setShowsSelectionIndicator:YES];
txtText.inputView = cityPicker;
// Create done button in UIPickerView
UIToolbar* mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
[mypickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDoneClicked)];
[barItems addObject:doneBtn];
[mypickerToolbar setItems:barItems animated:YES];
txtText.inputAccessoryView = mypickerToolbar; // set the toolbar as input accessory view for uitextfield, not the pickerview.
http://technopote.com/how-to-make-multiple-uipickerview-in-a-single-view/
#import "ViewController.h"
#interface ViewController ()<UIPickerViewDelegate>
{
UIPickerView *myPickerView;
NSArray *namesArray ;
}
#end
#implementation ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
namesArray=[[NSArray alloc]initWithObjects:#"a",#"b", nil];
[self popoverWithInformation];
}
-(void)popoverWithInformation
{
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(pickerCancel:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
myPickerView = [[UIPickerView alloc] init];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
CGRect pickerRect = myPickerView.bounds;
myPickerView.bounds = pickerRect;
myPickerView.frame = CGRectMake(0, 44, 320, 216);
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 300)];
popoverView.backgroundColor = [UIColor whiteColor];
[popoverView addSubview:myPickerView];
[popoverView addSubview:pickerToolbar];
[self.view addSubview:popoverView];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
}
// tell the picker how many rows are available for a given component
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return namesArray.count;
}
// tell the picker how many components it will have
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return namesArray[row];
}
// tell the picker the width of each row for a given component
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
-(void)pickerDone:(id)sender
{
}
-(void)pickerCancel:(id)sender
{
}
In Swift 5 try this,
func setToolBar() {
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 44))
toolBar.barStyle = UIBarStyle.default
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: self, action: #selector(self.donePicker))
toolBar.setItems([doneButton],animated: false)
toolBar.isUserInteractionEnabled = true
doneButton.tintColor = UIColor.black
picker.addSubview(toolBar)
}
#objc func donePicker() {
// hide keypad
}

UIPickerView doesn't "snap" to rows and moves out of bounds

I have a problem with UIPickerView. Following a tutorial on YT I created a UIPickerView in a popover after tapping on a textfield.
Everything looks fine, but the UIPickerView acts kind of strange. If I just tap on a row i want to select, it rolls up/down normally to this specific row and makes a selection. But if I try to scroll with my finger it doesn't roll as smooth as it should with a momentum and it doesn't snap on any of the rows. It also doesn't make a selection, but it makes a sound like it does. Worst of all, the picker doesn't seem to care about the number of rows and I can roll up and down as far as I want, although there are no selectable rows anymore.
Here is my code.
decayStatesArray contains four simple String-objects. The UITextfield und the activeDecayString are used in the "donePressed"-methode.
The main function:
-(void)showPicker:(UITextField *)textField {
activeTextfield = textField;
//for the default sizes of a UIPickerView
decayStatePicker = [[UIPickerView alloc] init];
UIViewController *popOverContent = [[UIViewController alloc] init];
UIView *popOverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [decayStatePicker frame].size.width, [decayStatePicker frame].size.height+44)];
decayStatePicker = [[UIPickerView alloc] initWithFrame:CGRectMake([popOverView frame].origin.x, [popOverView frame].origin.y + 44, 0, 0)];
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake([popOverView frame].origin.x, [popOverView frame].origin.y, [popOverView frame].size.width, 44)];
[decayStatePicker setDataSource:self];
[decayStatePicker setDelegate:self];
[decayStatePicker setShowsSelectionIndicator:YES];
[decayStatePicker selectRow:0 inComponent:0 animated:YES];
//Create Toolbar
[pickerToolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(donePressed)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelPressed)];
[pickerToolbar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, cancelButton, nil]];
[popOverView addSubview:decayStatePicker];
[popOverView addSubview:pickerToolbar];
[popOverContent setView:popOverView];
pickerPopoverController = [[UIPopoverController alloc] initWithContentViewController:popOverContent];
pickerPopoverController.popoverContentSize = CGSizeMake([popOverView frame].size.width, [popOverView frame].size.height);
[pickerPopoverController presentPopoverFromRect:[textField frame] inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
The delegate methodes:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
return [decayStatesArray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [decayStatesArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if ([pickerView isEqual:decayStatePicker]) {
NSLog(#"The Picker is: %#", pickerView);
NSLog(#"The item is: %#", [decayStatesArray objectAtIndex:row]);
}
activeDecayString = [decayStatesArray objectAtIndex:row];
}

change button image from UIPickerView

I have a question about something I've been working on for a while. I have a UIPickerView that lists different types of food. My array looks like such:
food = [[NSMutableArray alloc] init];
[activities addObject:#"Pasta"];
[activities addObject:#"Pizza"];
[activities addObject:#"Spaghetti"];
[activities addObject:#"Salad"];
And my image array is:
foodImages = [[NSMutableArray alloc] init];
[activities addObject:#"Pasta.png"];
[activities addObject:#"Pizza.png"];
[activities addObject:#"Spaghetti.png"];
[activities addObject:#"Salad.png"];
What I am trying to find out is if when the user selects an item from the picker view if I can change the image of a button. So if pasta is selected from the UIPickerView it would change the image of the button to "Pasta.png". How could I go about achieving this in Xcode?
You can certainly do that. First of all though, it would be much better to store the image links and the food name in one array. This will make your app more scalable and changes to the code are less likely to cause errors.
You could do this like so:
food = [[NSMutableArray alloc] init];
[food insertObject:[NSMutableArray arrayWithObjects:#"Pasta",#"Pasta.png",nil] atIndex:[food count]];
[food insertObject:[NSMutableArray arrayWithObjects:#"Pizza",#"Pizza.png",nil] atIndex:[food count]];
Though if your images are the same as your names but with .png extension then do you really need two objects?
As for setting the images for a UIPicker you would do something like this (You need to add UIPickerViewDelegate to the .h and call [self setPickerValue]; where ever you want to show your UIPicker - perhaps a button or text field?):
-(void)setPickerValue {
pickerActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
[pickerActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
UIPickerView *valuesPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
[valuesPicker setDataSource: self];
[valuesPicker setDelegate: self];
valuesPicker.showsSelectionIndicator = YES;
[pickerActionSheet addSubview:valuesPicker];
UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[controlToolBar setBarStyle:UIBarStyleBlack];
[controlToolBar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:#"Set" style:UIBarButtonItemStyleDone target:self action:#selector(dismissPicker)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelPicker)];
[controlToolBar setItems:[NSArray arrayWithObjects:cancelButton, spacer, setButton, nil] animated:NO];
[pickerActionSheet addSubview:controlToolBar];
[pickerActionSheet showInView:self.view];
[pickerActionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(void)cancelPicker {
[pickerActionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
// Number of components.
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
// Total rows in our component.
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [food count];
}
// Display each row's data.
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [[food objectAtIndex: row] objectAtIndex:0];
}
The above code makes a nice picker view with a cancel and ok button as a toolbar. The final function you need is (presuming you have a UIButton named "button1"):
-(void)dismissPicker {
[button1 setImage:[UIImage imageNamed: [[food objectAtIndex:row] objectAtIndex:1]] forState:UIControlStateNormal];
[pickerActionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
[yourButton setImage:[UIImage imageNamed:[foodImages objectAtIndex:0]] forState:UIControlStateNormal]; // objectAtIndex 0 will return "Pasta.PNG", 1 will return Pizza, etc.
Implement the UIPickerViewDelegate method (docs):
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *imageName = foodImages[row];
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}
Set your picker's delegate:
pickerView.delegate = self;
Then add your images to your Xcode project (if you haven't done so already).
You may also want to set the image for different states, or use setBackgroundImage:forState if you also want text on the button (see UIButton docs for more info).
I'd use a NSDictionary with the food's name being the Key and the food's picture file being the value. Then when it is selected, just load the Key's value as the picture.
If you want to stick with two arrays, then get the index of the selected food and as long as your foodImages array mirrors the food array, you can use that value for the objectAtIndex to load the image.

Add a UIButton to UIDatePicker

I have a UITextField that I set the inputView to a UIDatePicker so that when the UITextField is pressed, instead of a keyboard, a UIDatePicker pops up. I would like to also add a toolbar with a 'Next' button on it so that the user could easily jump to the next field, but I am not sure how to do this as I am already altering a view on the UITextField to get the UIDatePicker. Here is the code used in loading the view:
- (void)viewDidLoad
{
self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.datePickerMode = UIDatePickerModeDate;
[self.datePicker addTarget:self action:#selector(datePickerValueChanged) forControlEvents:UIControlEventValueChanged];
issue.inputView = self.datePicker;
}
Suggestions for getting the 'Next' button added to a toolbar on top of it?
Create a view to use as the text field's inputAccessoryView. This view should contain your buttons (e.g. Next, Cancel, Previous).
Here's the code :
if (keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
[keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleDone target:self action:#selector(switchToNextField:)];
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, next, nil]];
}
issue.inputAccessoryView = keyboardToolbar;
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
issue.inputView = datePicker;
And in your switchToNextField method, just make the nextField as the firstResponder using [nextTextField becomeFirstResponder];
You can create a view then put any UI elements inside of it.
#property (nonatomic,strong) UIPopoverController *popoverControllerBirthday;
add above to your .h file and sync it at .m #synthesize popoverControllerBirthday;
also change your #synthesize datePicker; to #synthesize datePicker=_datePicker;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField==self.yourtextfield) {
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
_datePicker=[[UIDatePicker alloc]init];//Date picker
_datePicker.frame=CGRectMake(0,0,320, 216);
_datePicker.datePickerMode = UIDatePickerModeDate;
[_datePicker setMinuteInterval:5];
[_datePicker setTag:10];
[popoverView addSubview:_datePicker];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(result)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Next" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 220, 320, 45);
UIColor *titleColor = [UIColor whiteColor];
[button setTitleColor:titleColor forState:UIControlStateNormal];
// UIImage *doneButtonImage = [UIImage imageNamed:#"create event button.png"]; choose a button background if you ahve one
[button setBackgroundImage:doneButtonImage forState:UIControlStateNormal];
[popoverView addSubview:button];
popoverContent.view = popoverView;
popoverControllerBirthday = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverControllerBirthday.delegate=self;
CGRect myFrame =textField.frame;
myFrame.origin.x = 200;// your coordinates change it as you wish
myFrame.origin.y = 195;
[popoverControllerBirthday setPopoverContentSize:CGSizeMake(320, 265) animated:NO];
[popoverControllerBirthday presentPopoverFromRect:myFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
return NO; // tells the textfield not to start its own editing process (ie show the keyboard)
}
else{
return YES;
}
}
//handle your result call next uitextfield in your case
-(void) result
{
//in result method you can get date using datepicker.date
if (self.popoverControllerBirthday)
{
[self.popoverControllerBirthday dismissPopoverAnimated:NO];
self.popoverControllerBirthday = nil;
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"M d yyyy"];
NSString *parseDatePickerDAte =[dateFormatter stringFromDate:_datePicker.date]; // your string that is choosen at datepicker
// self.textfield.text = parseDatePickerDAte ; set this to your textfield if you want
//google this dont remember exact code for that, just changing your textfield to desired one
[self.nexttextfield becomesFirstResponder];
}
You can use SMDatePicker instead of UIDatePicker. It will create a custom view with UIToolbar and UIDatePicker. You can easy customise toolbar with necessary buttons. Take a look at example project.

Resources