Date and Time in UIActionsheet Picker - ios

Hi I have a requirement where I need to show date and time in uiactionsheet on an iPad only app.
My google /sof search lead me to the following resource https://github.com/TimCinel/ActionSheetPicker
ActionSheetDatePicker *actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:#"" datePickerMode:UIDatePickerModeDateAndTime selectedDate:[NSDate date] target:self action:#selector(onDateSelected:element:) origin:sender];
actionSheetPicker.hideCancel = YES;
[actionSheetPicker showActionSheetPicker];
It works great except for the life of me I can't find where I can set the minimum , maximum date and intervals.
Please if any one has used this could help that be great. Thank you

Here i am using Actionsheet + UIDatePicker for iPhone/IPod, BUT for iPAD using POPOVERCONTROLLER + UIDATEPICKER.
- (IBAction)showAction:(id)sender
{
aac = [[UIActionSheet alloc] initWithTitle:#""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
self.dtPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(DatePickerCancelClick:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(DatePickerDoneClick:)];
[barItems addObject:doneBtn];
[pickerDateToolbar setItems:barItems animated:NO];
if (IS_IPHONE) {
[aac addSubview:pickerDateToolbar];
[aac addSubview:dtPicker];
[self.view addSubview:aac];
}
else {
UIView *view = [[UIView alloc] init];
[view addSubview:pickerDateToolbar];
[view addSubview:dtPicker];
UIViewController *vc = [[UIViewController alloc] init];
[vc setView:view];
[vc setContentSizeForViewInPopover:CGSizeMake(320, 260)];
popover = [[UIPopoverController alloc] initWithContentViewController:vc];
popover.delegate = self;
[popover presentPopoverFromRect:myButton.bounds inView:myButton
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
//Hope you got it. Thanks

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDateAndTime;
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 300)];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = -100;
pickerView.bounds = pickerRect;
Above code is useful for add UIDatePicker to UIActionSheet. In above code you can display Date and time by using UIDatePicker.

I ended up using this bit of code as per ibiren's suggestion from UIDatePicker in UIPopover thread .
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
UIDatePicker *datePicker=[[UIDatePicker alloc]init];//Date picker
datePicker.frame=CGRectMake(0,44,320, 216);
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[datePicker addTarget:self action:#selector(Result) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate=self;
[popoverContent release];
[popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
[popoverController presentPopoverFromRect:tempButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
//tempButton.frame where you need you can put that frame

I know this answer came up late. But for anyone who would like to use this library…
The new version of ActionSheetDatePicker has the following properties:
NSDate *minimumDate;
NSInteger minuteInterval;
NSCalendar *calendar;
NSTimeZone *timeZone;
NSLocale *locale;
that allow you to modify the basic options of DatePicker as you want.

Related

UIDatePicker does not pop up

In my app, when the user clicks the UITextField, the UIDatePicker should pop up. However, it is not popping up.When I run it, it just shows 'select the date!' on an action sheet and nothing else.
This is my code from my ViewController.m;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[pd resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component
pickerViewDate = [[UIActionSheet alloc] initWithTitle:#"Select the date!"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 210, 320, 216)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your specific mode
datePicker.backgroundColor = [UIColor whiteColor];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:#"dd.MM.yyyy"]; //desired format
[datePicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker
//now preparing the toolbar which will be displayed at the top of the datePicker
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleDefault;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
//UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
//[barItems addObject:flexSpace]; // set the left of the bar
[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
}
-(IBAction)dateChanged{
NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
[FormatDate setLocale: [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US"]];
[FormatDate setDateFormat:#"dd.MM.yyyy"];
pd.text = [FormatDate stringFromDate:[datePicker date]];
}
-(BOOL)closeDatePicker:(id)sender{
[pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
[pd resignFirstResponder];
return YES;
}
-(IBAction)doneButtonClicked{
[self closeDatePicker:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Please I just need help with knowing what I am doing wrong.
Read the description of UIActionSheet in the Xcode docs. It says:
UIActionSheet is not designed to be subclassed, nor should you add
views to its hierarchy.
That last bit is important. You can't add a picker view to an action sheet because you're not supposed to add any views to an action sheet.
BTW, UIActionSheet is was in iOS 8, and we are about to move to iOS 9, so it will have been deprecated for 2 major releases as of this week. Thus you should not be doing new development with it. I suggest you create your own view controller and present it modally rather than trying to use UIActionSheet.
You forgot to show UIActionSheet at the end of -(void)textFieldDidBeginEditing:(UITextField *)textField:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[pd resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component
pickerViewDate = [[UIActionSheet alloc] initWithTitle:#"Select the date!"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 210, 320, 216)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your specific mode
datePicker.backgroundColor = [UIColor whiteColor];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:#"dd.MM.yyyy"]; //desired format
[datePicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker
//now preparing the toolbar which will be displayed at the top of the datePicker
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleDefault;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
//UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
//[barItems addObject:flexSpace]; // set the left of the bar
[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
[pickerViewDate showInView:self.view]; // <---- here, maybe you forgot
}

Why not see the buttons?

The task is to choose the date for a text field. The development is on the iPad, so I use UIPopover. But I need two buttons on top. I'm trying to do it, but the buttons are not displayed. In what could be the problem?
Please see screenshot:
And the full code:
dateSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[dateSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
UIDatePicker *dayPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[dayPicker setDatePickerMode:UIDatePickerModeDate];
[dateSheet addSubview:dayPicker];
[dayPicker release];
UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlackTranslucent];
[controlToolBar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton;
setButton = [[UIBarButtonItem alloc] initWithTitle:#"Установить" style:UIBarButtonItemStyleDone target:self action:#selector(dismissDateStart:)];
setButton.tag = pTag;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Отменить" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelDateStart)];
[controlToolBar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil] animated:NO];
[spacer release];
[setButton release];
[cancelButton release];
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 344)];
popoverView.backgroundColor = [UIColor whiteColor];
dateSheet.frame = CGRectMake(0, 0, 320, 344);
[popoverView addSubview:dateSheet];
[popoverView addSubview:controlToolBar];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 244);
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController presentPopoverFromRect:changeDateStartField.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[popoverView release];
[popoverContent release];
You should change the
UIBarButtonSystemItemFlexibleSpace
to
UIBarButtonSystemItemFixedSpace
of your spacer button

how to create pickerview in storyboard as popover with done button on uitoolbar

I have created the above example using code but was wondering if same can be achieved using storyboard.
I am a noob in ios and would like to know how to go about it.
Thanks in Advance.
Code without storyboard and done button:
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
UIDatePicker *datePicker=[[UIDatePicker alloc]init];//Date picker
datePicker.frame=CGRectMake(0,44,320, 216);
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[datePicker addTarget:self action:#selector(Result) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate=self;
[popoverContent release];
[popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
[popoverController presentPopoverFromRect:tempButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];//tempButton.frame where you need you can put that frame

ios 6: UIDatePicker in the UITextField

I have problems with connecting datepicker with text field. I have tried all solution from stackoverflow but they don't work in ios 6. Can you desribe how to make it, I want to tap on text field and choose date via datepicker.
You should use the inputView property of your UITextField to make the picker show in place of the keyboard. iOS6 will take care of everything for you then (showing the picker with the animation instead of the keyboard, etc).
Note: You will probably want to add some inputAccessoryView too (generally an UIToolBar with some "OK" button in it) to make the user be able to dismiss the picker (the IBAction of your "OK" button will simply call [textField resignFirstResponder] for that to happen of course) as an UIDatePicker does not have any button to validate your input (whereas the keyboard has its "Return Key")
#import "TextfieldwithDatepickerViewController.h"
UIActionSheet *pickerViewPopup;
#implementation TextfieldwithDatepickerViewController
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{
[aTextField resignFirstResponder];
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar 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(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
}
-(void)doneButtonPressed:(id)sender{
//Do something here here with the value selected using [pickerView date] to get that value
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
-(void)cancelButtonPressed:(id)sender{
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}

Pickerview for TextField

I have this code, it's inside an action that it's triggered when the user selects the textfield to show the datepicker:
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDateAndTime;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showFromTabBar:self.tabBarController.tabBar];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 500)];
I was using this code in an app with a tab bar for navigation. Now I would like to use it in an app without it, but when the process reaches [pickerViewPopup showFromTabBar:self.tabBarController.tabBar]; it crashes.
Any idea on how I could fix it?
Use one of the following,
– showFromToolbar:
– showInView:
– showFromBarButtonItem:animated:
– showFromRect:inView:animated:
For eg:-
[pickerViewPopup showFromToolbar:pickerToolbar];
The problem was that you dont have a tab bar and you are trying to access the tab bar object to show the picker from it. Since tabbar is nil for you, it will crash.
For more details check the apple documentation.

Resources