I have implemented datepicker in one of my view,
The way is added the datepicker to the Alertview. Now in iOS 7 i am not able to find the datepicker on the alertview, When searched i have got, we cannot add anything to alertview in iOS7.
My requirement was When i will select the DOB textfield, I have to show the Datepickerview and modify the date and the changed value will be stored into the textfield.
Now my problem is How can i show the datepicker on the alertview.
I have tried showing on the view, but when trying to remove the datpicker from view it is not working.
I know this is an older question, but I believe the following allows what the OP requested and I don't think will violate Apple's rules involving AlertView hierarchy changes:
- (void)requestDateOfBirth
{
UIDatePicker *birthDatePicker;
UIAlertView *setBirthDate;
NSDateFormatter *birthDateFormatter;
UITextField *birthDateInput;
//create the alertview
setBirthDate = [[UIAlertView alloc] initWithTitle:#"Date of Birth:"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
setBirthDate.alertViewStyle = UIAlertViewStylePlainTextInput;
//create the datepicker
birthDatePicker = [[UIDatePicker alloc] init];
[birthDatePicker addTarget:self action:#selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
birthDatePicker.datePickerMode = UIDatePickerModeDate;
birthDatePicker.date = [NSDate date];
//get the textfield provided in the plain text alertview
birthDateInput = [setBirthDate textFieldAtIndex:0];
//change the textfields inputView to the date picker
birthDateInput.inputView = birthDatePicker;
[birthDateInput setTextAlignment:NSTextAlignmentCenter];
//create a date formatter to suitably show the date
birthDateFormatter = [[NSDateFormatter alloc] init];
[birthDateFormatter setDateStyle:NSDateFormatterShortStyle];
//set the current date into the textfield
birthDateInput.text = [birthDateFormatter stringFromDate:[NSDate date]];
//show the alert view and activate the textfield
[setBirthDate show];
[birthDateInput becomeFirstResponder];
}
Then don't forget the handle the changes in the date picker
- (void) dateChanged:(id)sender
{
NSDateFormatter *birthDateFormatter;
UIDatePicker *birthDatePicker = (UIDatePicker *)sender;
birthDateFormatter = [[NSDateFormatter alloc] init];
[birthDateFormatter setDateStyle:NSDateFormatterShortStyle];
birthDateInput.text = [birthDateFormatter stringFromDate:birthDatePicker.date];
}
I hope this helps
In iOS 7 you can use
[alertView setValue:yourDataPicker forKey:#"accessoryView"];
Full explanation is here
The hierarchy of UIAlertView is private and should not be modified.
From Apple docs
The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
Until iOS 6, you could add some views to the UIAlertView subviews hierarchy, but this doesn’t seems to work anymore on iOS 7. The contentView property that was announced for iOS7 was made a private ivar since beta4 release, thus making it impossible to addSubview to alert view.
UIView *_contentViewNeue;
For more on this refer this Apple dev forum discussion (You will need to login)
In such situations, you should create a custom view mimicking the UIAlertView and then use that to add the picker view.
Hope that helps!
Related
I want to display a picker view and a done button above the picker view .
I have a custom label which will become first responder . So when it becomes first responder I am displaying a picker view as the input view for my label.
Now I want to have a done button on the picker to dismiss the picker view. I played with the inputAccesoryView property of UIPickerView but did not succeed . inputAccessoryView is possible with UITextView , not sure about the UIPickerView . Here is the link for how it can be done with UITextView
UITextView with “Done” button and “Return” key?
Has any body tried this , if someone knows how to do this for picker , please answer.
Thank you all in advance !!
Add your picker view to an action sheet. First of all make sure youre view controller impliments UIPickerViewDelegate and UIActionSheetDelegate.
Then add this method, and call it when you want to show your picker.
- (void)showPicker {
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Pick Value"
delegate:self
cancelButtonTitle:#"Done"
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,180,0,0)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
[menu addSubview:pickerView];
[menu showInView:self.view.superview];
//Change the height value in your CGRect to change the size of the actinsheet
[menu setBounds:CGRectMake(0,0,320, 615)];
[pickerView release];
[menu release];
}
I think you can try to put the picker inside a view with a 'Done' button and present that view
I'm trying to get a datepicker in countdowntimer mode to simply allow me to select minutes and seconds and then get that value when you click a button. I set up the picker in IB and use this code, but the result is always Null when I select any combination of minutes and hours and click the button...Obviously I'm missing something. Any help on getting this to work would be appreciated.
#interface ThisPicker : UIViewController {
IBOutlet UIDatePicker *datePicker;
}
-(IBAction)buttonPressed:(id)sender;
#end
#implementation ThisPicker
-(IBAction)buttonPressed:(id)sender{
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
NSLog(#"%#",[datePicker countDownDuration]);
}
#end
If you want in a Hour Minute format
-(IBAction)buttonPressed:(id)sender
{
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"HH:mm:ss"];
NSLog(#"%#",[dateformatter stringFromDate:[datePicker date]]);
}
I believe that countDownDuration is a double.
Try accessing it this way:
NSLog(#"%f", datePicker.countDownDuration);
Edit: What you need to do is link your date picker in IB with the one in code and don't alloc it as it's causing an erase of your data.
Edit2: BuildSucceeded beat me to it. :)
I want a UIPicker to popup instead of the keyboard when I click on a text field. Is this possible, I've been googling for awhile but can't find anything.
Does anyone have any sample code ?
Regards,
Stephen
I think this is a more official way to do this:
UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
datePicker.tag = indexPath.row;
textField.inputView = datePicker;
Use [self presentModalViewController:picker] to present a modal view controller when user click on the text field. I hoped that you know how to handle text field event
I have a UIView with a date picker that I'd like to display in an action sheet. I'm using the following code:
-(IBAction) button_click:(id)sender{
//UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"the title" delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Destructive" otherButtonTitles:#"other", nil];
UIActionSheet *sheet = [[UIActionSheet alloc] init];
ActionSheetController *actionSheet = [[ActionSheetController alloc] initWithNibName:#"ActionSheetView" bundle:nil];
[sheet addSubview:actionSheet.view];
[sheet showInView:self.view];
}
What I get is a little bit of the top part of the new view coming up from the bottom and that's it. If I comment the two middle lines of code and uncomment the top part to display a regular action sheet, it works fine. Any ideas what I might be doing wrong?
use UIActionSheetDelegate and after that you can with this delegate add for example a image to button of alertsheet
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet{
UIImageView *sample = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:#"Someimage.png"]];
[actionSheet addSubView:sample];
}
I do not believe you can specify Custom Views in a UIActionSheet. My understanding is that you can only display a title and 1 or more buttons. Below is the description of UIActionSheet in the Apple documentation:
Use the UIActionSheet class to present the user with a set of alternatives for how to proceed with a given task. You can also use action sheets to prompt the user to confirm a potentially dangerous action. The action sheet contains an optional title and one or more buttons, each of which corresponds to an action to take.
UIActionSheet Class Reference
I've discovered the behavoir occurs because I haven't used the UIActionSheet constructor properly. The buttons and title should be included:
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
You might want to check out an UIActionSheet alternative I made which lets you display custom content views without any dirty hacks: https://github.com/JonasGessner/JGActionSheet It has some other advantages over UIActionSheet as well!
I am implementing datepicker with Action Sheet with the following code :
-(void)popupActionSheetWithDatePicker
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:#"Done",nil];
UIDatePicker *picker = [[UIDatePicker alloc] init];
self.datePicker = picker;
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
[sheet addSubview:self.datePicker];
[sheet showInView:self.view];
[sheet setBounds:CGRectMake(0,0, 320,500)];
CGRect pickerRect = self.datePicker.bounds;
pickerRect.origin.y = -80;
self.datePicker.bounds = pickerRect;
[sheet bringSubviewToFront:self.datePicker];
[picker release];
[sheet release]; }
Now, The Picker pops up & working perfactly Except below the Selection Indicator.
I mean On & Above the Selection indicator I am able to choose date, month & year, but Below that Selection indicator I can't select anything, It's become like disabled.
Can't find the reason or solution.
If Possible Please send any working Datepicker with UIActionsheet code.
Thanks in advance.
I have had a very similar issue with parts of a view not working when used with a UIActionSheet. My problem was that the bounds of the UIActionSheet were larger than the view I had added it to.
My solutions was to change where i inserted the sheet
[sheet showInView:self.view];
You may need to add it higher up for example
[sheet showInView:self.view.superview];
Hope this at least sets you on the right track
I had this problem also , i supose you have an UIToolbar behind the picker. Just do
[self.navigationController setToolbarHidden:YES];
while in picker , then
[self.navigationController setToolbarHidden:NO];
hope this help