2

I have an application in which I need to display a popup to pick a date and write that date back to the view controller.

I have searched online a lot but wasn't successful in finding out a relevant solution.

Any kind of help is much appreciated.

Srujan Simha
  • 3,637
  • 8
  • 42
  • 59
  • `UIAlertView` is deprecated in the iOS 8 SDK. Do you mean a `UIAlertController`? If so, this question may help: http://stackoverflow.com/questions/25545982/is-there-any-way-to-add-uipickerview-into-uialertcontroller-alert-or-actionshee – JAL Nov 05 '14 at 21:45
  • @JAL Yes I meant UIAlertController. The link you have given is not about Swift (I mean the answers users gave for the question). I don't know Objective-C, I started with Swift so it's kind of difficult for me to convert that code to Swift. – Srujan Simha Nov 05 '14 at 21:54

2 Answers2

14

Do not use any kind of UIAlertView or UIAlertController. Make your own view and pop it up (using, probably, a presented view controller). The view can be small, like an date picker, and you can put a shadow view behind it, like an alert does.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • This example will get you started: https://github.com/mattneub/custom-alert-view-iOS7 It doesn't involve a date picker, but it shows how to turn _any_ view into something that looks and acts like an alert. – matt Nov 05 '14 at 22:55
0

This is my Code :

- (id)initWithDatePicker:(NSString*)title parentView:(UIView*)parentView {
 self = [super init];

 if (self) {
     datePickerView = [[UIDatePicker alloc] init];
     datePickerView.datePickerMode = UIDatePickerModeDateAndTime;
     if (IS_IOS8_AND_UP) {
         alertViewController = [UIAlertController
                                alertControllerWithTitle:EMPTY_STRING
                                message:title
                                preferredStyle:UIAlertControllerStyleActionSheet];
         UIView* aboveBlurLayer = alertViewController.view.subviews[0];
         [aboveBlurLayer addSubview:datePickerView];
         [aboveBlurLayer setWidth:SCREEN_WIDTH - 16];
         [datePickerView setWidth:SCREEN_WIDTH - 16];
         [alertViewController.view setWidth:SCREEN_WIDTH - 16];
         [alertViewController.view setBackgroundColor:[UIColor whiteColor]];

         UIAlertAction* alertAction =
         [UIAlertAction actionWithTitle:EMPTY_STRING
                                  style:UIAlertActionStyleDefault
                                handler:nil];
         [alertViewController addAction:alertAction];
         [alertViewController addAction:alertAction];
         [alertViewController addAction:alertAction];
         [alertViewController addAction:alertAction];

         [datePickerView setBackgroundColor:[UIColor whiteColor]];
         [aboveBlurLayer addSubview:datePickerView];
     } else {
         actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                                   delegate:self
                                          cancelButtonTitle:nil
                                     destructiveButtonTitle:nil
                                          otherButtonTitles:nil];

         [actionSheet addSubview:datePickerView];
     }

     [self addToolBar];
     isDatePicker = YES;
     parent = parentView;
 }   
  return self; 
}

On Tool bar I have two buttons Done and Cancel On Done i send back a call via delegate with selected date and on cancel I dismiss. This code is for both iOS 7 and iOS 8

Reconquistador
  • 885
  • 8
  • 28
Zeeshan
  • 4,194
  • 28
  • 32