2017-08-08 64 views
2

我有一个自定义警报视图控制器来从日期选择器中选择日期,但我很困惑,我怎么能在警报视图控制器中添加两个按钮和当用户单击按钮比它显示警报视图控制器中的日期选择器,以及如何更改警报视图控制器的颜色。我的代码,自定义警报视图控制器在IOS中选择日期

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]; 

它看起来像这样, enter image description here

+0

如果你看看docs for UIAlertViewController https://developer.apple.com/documentation/uikit/uialertcontroller你会发现你真的不应该对它的视图层次结构进行讨论。你只是在为自己排忧解难。 – Abizern

回答

0

如果我记得很清楚这个问题,因为已经问过一段时间,每次得到的答复是“你应该创建一个视图控制器,并使用它作为警告”,看见你需要做的我也认为这是一个更好的办法,+你可以让你的控制器看起来像一个警报添加阴影什么...

看到这个答案:How to add date picker to UIAlertController(UIAlertView) in Swift?

+0

我正在工作在目标c –

+0

@HamzaImran这将是正确的方法,即使你在obj c工作。在alertview中添加太多的组件会让它变得糟糕。 – Rishabh

+0

我可以在目标c中获得吗? @Rishabh –

相关问题