2

I have a custom alert view controller to select date from date picker, but i'm confuse bit that how can i add two button in alert view controller and when user click the button than it shows the date picker in alert view controller and how can i change the color of alert view controller. My code is ,

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIDatePicker *picker = [[UIDatePicker alloc] init];
[picker setDatePickerMode:UIDatePickerModeDate];
[alertController.view addSubview:picker];
[alertController addAction:({
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"OK");
        NSLog(@"%@",picker.date);
    }];
    action;
})];
UIPopoverPresentationController *popoverController = alertController.popoverPresentationController;
popoverController.sourceView = sender;
popoverController.sourceRect = [sender bounds];
[self presentViewController:alertController  animated:YES completion:nil];

It looks like this, enter image description here

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Hamza Imran
  • 189
  • 1
  • 18
  • If you look at the docs for UIAlertViewController https://developer.apple.com/documentation/uikit/uialertcontroller you'll see that you really aren't supposed to futz around with it's view hierarchy. You're just making trouble for yourself down the line. – Abizern Aug 08 '17 at 10:03

1 Answers1

0

If I remember well this question as already been asked few time , and each time the answer was "you should create a viewcontroller and use it as an alert" and , seeing what you need to do I also think it's a better way to do, + you can make your controller look like an alert adding a shadow...

See this answer : How to add date picker to UIAlertController(UIAlertView) in Swift?

Leo
  • 68
  • 9