I am working on a project which I had already released with iOS-7. But now as the action sheet is making problem so I am now implementing UIAlertController. Following is the code which I am using for showing UIAlertController with UIPicker.
alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:alertAction];
[alertController addAction:alertAction];
[alertController addAction:alertAction];
[alertController addAction:alertAction];
[pickerToolbar setFrame:CGRectMake(0, 0, self.view.frame.size.width-20, 44)];
[alertController.view addSubview:pickerToolbar];
[alertController.view addSubview:picker];
[alertController.view setBounds:CGRectMake(0, 0, self.view.frame.size.width, 485)];
[self presentViewController:alertController animated:YES completion:nil];
But I am not able to add datepicker in it. It is not working same as normal picker and the app hang. Please advice for any sample code for adding UIDatePicker in UIAlertController. Thanks in advance
Adding UIDatePicker to action sheet was discouraged by Apple all along. Since iOS 7, Apple had introduced the use of inline date picker (see how it is done in the Calendar app).
If you managed to hack a workaround using UIAlertController, it may probably break again in future iOS releases.
For iOS9+ Swift code here.
add target to your date button or other view.
dateBtn.addTarget(self,action: #selector(YourViewController.dateSelect(_:)),forControlEvents: UIControlEvents.TouchDown) //my date button
func dateSelect(sender:UIButton) {
if (UI_USER_INTERFACE_IDIOM() == .Phone)
{
//init date picker
self.datePicker = UIDatePicker(frame: CGRectMake(0, 0, self.view.frame.size.width,260))
self.datePicker.datePickerMode = UIDatePickerMode.Date
//add target
self.datePicker.addTarget(self, action: #selector(YourViewController.dateSelected(_:)), forControlEvents: UIControlEvents.ValueChanged)
//add to actionsheetview
if(UIDevice.currentDevice().systemVersion >= "8.0")
{
let alertController = UIAlertController(title: "Date Selection", message:" " , preferredStyle: UIAlertControllerStyle.ActionSheet)
alertController.view.addSubview(self.datePicker)//add subview
let cancelAction = UIAlertAction(title: "Done", style: .Cancel) { (action) in
self.dateSelected(self.datePicker)
self.tableView.reloadData()
}
//add button to action sheet
alertController.addAction(cancelAction)
let height:NSLayoutConstraint = NSLayoutConstraint(item: alertController.view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 300)
alertController.view.addConstraint(height);
self.presentViewController(alertController, animated: true, completion: nil)
}
}
}
//selected date func
func dateSelected(datePicker:UIDatePicker)
{
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
let currentDate = datePicker.date
let day = currentDate.day()
let month = currentDate.month()
let year = currentDate.year()
let date = "\(day)/\(month)/\(year)"
print(date)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With