Enable the datepicker from a particular date - iphone

I am making an iphone app on one of the view I am using a date picker. I have an another date which I am fetching from the database. Now I want that the date picker enable from the selected date so that user couldn't get unable to select the same date again. Is there any way to enable the picker from selected date. If anyone know please help me.
-(void)findextenddate
{
NSLog(#"count:%#",adddatetime);
addatimeclass = adddatetime;
endnsdate = [addatimeclass objectAtIndex:[addatimeclass count]-1];
endnsdate1 = (NSDate*) endnsdate;
NSLog(#"formatdate:%#",endnsdate1);
}
-(IBAction)clicktoextend:(id)sender
{
actionsheet = [[UIActionSheet alloc]init];
actionsheet = [[UIActionSheet alloc] initWithTitle:#"Select Date" delegate:self
cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Select", nil];
[actionsheet showInView:[self.view superview]];
[actionsheet setFrame:CGRectMake(0, 117, 320, 383)];
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
pickerView.minimumDate = endnsdate1 ;
pickerView.datePickerMode = UIDatePickerModeDate;
//Add picker to action sheet
[actionsheet addSubview:pickerView];
subviews = [actionsheet subviews];
[[subviews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(20, 266, 280, 46)];
[[subviews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 317, 280, 46)];
[pickerView release];
[actionsheet release];
}
Thanks alot.

Yes, UIDatePicker has minimumDate property which serves exactly that purpose

USE
datePicker.minimumDate=YOUR date object
this function disable all dates before your date

If you meant that you need to set the date of the picker to the one you are fetching from the database, you can do so via the - (void)setDate:(NSDate *)date animated:(BOOL)animated method.
If you need to enable the picker from that date ONWARDS (no past dates), set the minimumDate property as is suggested by the earlier answers.

Related

Make a date picker appear when a user taps a text field

Im trying to follow this post Display datepicker on tapping on textfield and when I get to the point where it says "In the XIB, Pull out a UIToolbar and a UIDatePicker but don't attach it to the view" i get lost. Im using storyboards and he's using xib's. Is that the problem? xib's aren't an option for me. So is there a way to drag a date picker out, but not attach to the view?
The difference with storyboard is that you would use a XIB file (also called NIB) for each view.
But you should be able to follow any instructions.
In the XIB, Pull out a UIToolbar and a UIDatePicker but don't attach
it to the view.
I believe he means not to connect to the ViewController like you would do with a button (IBAction for example)
Since you are showing when you tap the textfield then you have to do it programmatically.
You don't need to add it to your view really.
You can create a viewcontroller that has a date picker at the bottom and a close button at the top.
When tapping the textfield you can just present that viewcontroller modally.
Set delegate methods to the viewcontroller for:
closeButtonClicked: , and dateSelected: ,
and implemet these delegate merhod in the main controller.
In addition, you can "block" the screen by adding the datePicker's viewcontroller background color with certain alpha for a nice affect.
I found that to be the easiest way to do that. No need for a nib
May be it will helpful to you..
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
dateActionSheet = [[UIActionSheet alloc]initWithTitle:#"Select Your Date of Birth" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil , nil];
dateActionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChangedd:) forControlEvents:UIControlEventValueChanged ];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
[dateActionSheet addSubview:datePicker];
[dateActionSheet addSubview:self.datePickerView];
UIToolbar *tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,320,40)];
tools.barStyle=UIBarStyleBlackOpaque;
[dateActionSheet addSubview:tools];
UIBarButtonItem *doneButton=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActinDoneClicked)];
doneButton.imageInsets=UIEdgeInsetsMake(200, 6, 50, 25);
UIBarButtonItem *flexSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *array = [[NSArray alloc]initWithObjects:flexSpace,flexSpace,doneButton,nil];
[tools setItems:array];
//picker title
UILabel *lblPickerTitle=[[UILabel alloc]initWithFrame:CGRectMake(60,8, 200, 25)];
lblPickerTitle.text=#"Select Date of Birth";
lblPickerTitle.backgroundColor=[UIColor clearColor];
lblPickerTitle.textColor=[UIColor whiteColor];
lblPickerTitle.textAlignment = NSTextAlignmentCenter;
lblPickerTitle.font=[UIFont boldSystemFontOfSize:15];
[tools addSubview:lblPickerTitle];
//cell.detailTextLabel.text = [self datePickerValueChangedd:datePicker];
[dateActionSheet showFromRect:CGRectMake(0,480, 320,215) inView:self.view animated:YES];
[dateActionSheet setBounds:CGRectMake(0,0, 320, 411)];
}
-(void)btnActinDoneClicked {
[dateActionSheet dismissWithClickedButtonIndex:1 animated:YES];
[dateActionSheet removeFromSuperview];
}
-(void)datePickerValueChangedd:(UIDatePicker*) datePicker
{
Youytextfield.text = (NSString *)datePicker.date;
}

How to work with UITextFields inside of UIActionsheet?

i am new in iphone.i am doing reminder application to my project . i want get input values like reminder title and data&Time from uiactionsheet.i have created datepicker and uitextfield in actionsheet . The actionsheet appears with textfield.when i clicked on textfield,the keypad appears but not working means..when i was typed they are not visible in the textfield
-(void)btnSetRemainderTapped:(id)sender
{
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Select reminder date and time"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Save"
otherButtonTitles:nil,nil];
menu.tag =10;
[menu showInView:self.view];
menu.userInteractionEnabled=YES;
UITextField *txtReminderTitle= [[UITextField alloc] initWithFrame:CGRectMake(20.0, 180, 280.0, 40.0)];
[txtReminderTitle setTag:99];
[txtReminderTitle setBackgroundColor:[UIColor whiteColor]];
[txtReminderTitle setFont:[UIFont boldSystemFontOfSize:17]];
[txtReminderTitle setBorderStyle:UITextBorderStyleNone];
[txtReminderTitle setTextAlignment:UITextAlignmentCenter];
txtReminderTitle.borderStyle = UITextBorderStyleRoundedRect;
txtReminderTitle.keyboardType = UIKeyboardTypeDefault;
txtReminderTitle.returnKeyType = UIReturnKeyDone;
txtReminderTitle.delegate =self;
[menu addSubview:txtReminderTitle];
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDateAndTime;
pickerView.tag =9999;
[menu addSubview:pickerView];
CGRect menuRect = menu.frame;
NSInteger orgHeight = menuRect.size.height;
menuRect.origin.y -= 270; //height of picker
menuRect.size.height = orgHeight+270;
menu.frame = menuRect;
CGRect pickerRect = CGRectMake(20, 250, 280, 200);
pickerView.frame = pickerRect;
}
-(void) actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
UIDatePicker *datePicker = (UIDatePicker *)[actionSheet viewWithTag:9999];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterLongStyle;
[df setTimeStyle:NSDateFormatterShortStyle];
UITextField * txtReminderTitle = (UITextField *)[actionSheet viewWithTag:99];
NSLog(#"%#",txtReminderTitle.text); //it print null value
}
I'm not sure why it's not accepting input from the keyboard. Any suggestions?
I found that, when you add UITextField to UIActionSheet then UITextField is remain under the UIActionSheet. So you need to bring UITextField above UIActionSheet thats way
[menu addSubview:txtReminderTitle]; does not work for UITextField to input any text.
Use following code for add UITextField:
UIWindow *appWindow = [UIApplication sharedApplication].keyWindow;
[appWindow insertSubview:txtReminderTitle aboveSubview:menu];
Only need to do changes in how to add UITextField.
Use custom UIActionSheet . do like following
1) add new UIViewController
2) declare one UITextField
#interface ViewController
{
UITextField *TextField;
}
3) add delegates of ActionSheet & TextField
#interface ViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate> {
UITextField *TextField;
}
4)Now implement methods
see this link just i see on stackoverflow

iPhone:DatePicker dd/mm/yyyy

I am using a DatePicker with popups when a user click a textField. However, the Picker displays Month first, not date first and it isn't British way to write a date.
Is there any way to set the date format in DatePicker to British way such as dd/mm/yyyy?
I am using an Achtionsheet:
-(IBAction)acsheet:(id)sender
{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
//CGRect pickerFrame = CGRectMake(0, 45, 0, 0);
pickerView1 = [[UIDatePicker alloc] init];
pickerView1.datePickerMode = UIDatePickerModeDate;
[actionSheet addSubview:pickerView1];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:)
forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
[pickerView1 addTarget:self
action:#selector(updateLabel:)
forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissDatePicker:)] ;
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];
}
The UIDatePicker, by default, uses whatever [NSLocale currentLocale] dictates.
The locale property on UIDatePicker was deprecated in iOS 5, but I believe that you could do the following:
NSCalendar *cal = [NSCalendar currentCalendar];
cal.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
myDatePicker.calendar = cal;
This should work. If it doesn't, please file a bug and I'll fix it. :)
The UIDatePicker is made to adapt to the user's settings. Changing its format using the locale: method was possible but is now deprecated in iOS 5.0.
You can view your format in the settings of your iPhone :
Settings > General > International > Region Format
If you want a specific format, consider making your own picker but it is very discouraged for a date picker.
When writing the month in text the british probably would put the month first (or at least it would be acceptable).
But to answer your question I'm not aware of a way to change the display format of the date picker, but you could make you're own using a UIPickerView with a custom delegate and data source.

how to display only time in UIDatePicker iphone

In my app i am displaying a UIDateTimePicker in action sheet through code. it is working fine.
But i don't want to display date and day.How this can be done.
Please help. Thanks in advance.
code:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(#"Select Date",#"Selecte Date")delegate:self cancelButtonTitle:NSLocalizedString(#"Done",#"Done")
destructiveButtonTitle:NSLocalizedString(#"Cancel",#"Cancel")
otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 50, 320, 270)];
//datePicker.showsSelectionIndicator = YES;
// datePicker.dataSource = self;
//datePicker.delegate = self;
[actionSheet addSubview:datePicker];
[datePicker release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 500)];
[actionSheet release];
datePicker.datePickerMode = UIDatePickerModeTime;
Set the datePickerMode property to one of the following:
typedef enum {
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer
} UIDatePickerMode;
In Swift:
datePicker.datePickerMode = UIDatePickerMode.Time
datePicker.datePickerMode = UIDatePickerModeTime;

How to popup a view from bottom?

When click a button,I want to popup a view form bottom,like this!
Any help is welcome,thanks.
Use the presentModalViewController:animated: method. See the docs for details on its use.
If you want to reproduce what is seen in the screenshot you linked you can also put a picker in a UIActionSheet like so:
UIActionSheet datePickerSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(#"Select Date",#"Selecte Date")
delegate:self
cancelButtonTitle:NSLocalizedString(#"Done",#"Done")
destructiveButtonTitle:NSLocalizedString(#"Cancel",#"Cancel")
otherButtonTitles:nil];
// Add the picker
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,185,0,0)];
[datePicker addTarget:self action:#selector(dateDidChange) forControlEvents:UIControlEventValueChanged];
[datePickerSheet addSubview:datePicker];
[datePickerSheet showInView:self.view];
[datePickerSheet setBounds:CGRectMake(0,0,320, 700)];
[datePicker release];
[datePickerSheet release];