Wednesday, 19 November 2014

Swift + Open UIDatePicker with ActionSheet


// Compatible with iOS 7 and iOS 8 both



var datePicker: UIDatePicker!


override func viewDidLoad()
{
        super.viewDidLoad()
}



     //--- *** ---//

    func createDatePickerViewWithAlertController()
    {
        var viewDatePicker: UIView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, 200))
        viewDatePicker.backgroundColor = UIColor.clearColor()
        
        
        self.datePicker = UIDatePicker(frame: CGRectMake(0, 0, self.view.frame.size.width, 200))
        self.datePicker.datePickerMode = UIDatePickerMode.DateAndTime
        self.datePicker.addTarget(self, action: "datePickerSelected", forControlEvents: UIControlEvents.ValueChanged)
        
        viewDatePicker.addSubview(self.datePicker)
        
        
        if(UIDevice.currentDevice().systemVersion >= "8.0")
        {

            let alertController = UIAlertController(title: nil, message: "\n\n\n\n\n\n\n\n\n\n", preferredStyle: UIAlertControllerStyle.ActionSheet)
            
            alertController.view.addSubview(viewDatePicker)
            
            
            
            
            let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel)
                { (action) in
                    // ...
            }
            
            alertController.addAction(cancelAction)
            
            let OKAction = UIAlertAction(title: "Done", style: .Default)
                { (action) in
                    
                    self. dateSelected()
             }
            
            alertController.addAction(OKAction)
            
            /*
            let destroyAction = UIAlertAction(title: "Destroy", style: .Destructive)
            { (action) in
                 println(action)
            }
            alertController.addAction(destroyAction)
            */
            
            self.presentViewController(alertController, animated: true)
            {
                    // ... 
            }

        }
        else
        {
            let actionSheet = UIActionSheet(title: "\n\n\n\n\n\n\n\n\n\n", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done")
            actionSheet.addSubview(viewDatePicker)
            actionSheet.showInView(self.view)
        }

    }


    func dateSelected()
    {
        var selectedDate: String = String()
        
        selectedDate =  Self.dateformatterDateTime(self.datePicker.date)
        
        self.textFieldFromDate.text =  selectedDate

    }


   func dateformatterDateTime(date: NSDate) -> NSString
    {
        var dateFormatter: NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy h:mm a"
        return dateFormatter.stringFromDate(date)
    }





// Now Implement UIActionSheet Delegate Method just for support for iOS 7 not for iOS 8

    // MARK: - UIActionSheet Delegate Implementation ::
    
    func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int)
    {
        switch buttonIndex
        {
            
        case 0:
            println("Done")
            self. dateSelected()
            break;
        case 1:
            println("Cancel")
            break;
        default:
            println("Default")
            break;
        }
    }


2 comments:

  1. UIView is not displaying in alertController.

    ReplyDelete
  2. CHANGE viewDatePicker.backgroundColor = UIColor.clearColor()
    TO viewDatePicker.backgroundColor = UIColor.white / gray

    ReplyDelete