0

UIAlertview not adding UIDatePicker when i click on textfield?

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Preferences"
                                                message:@"\n\n\n\n\n\n\n"
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"OK", nil];
    pickerView=[[UIDatePicker alloc]initWithFrame:CGRectMake(0,40,246,150)];
    pickerView.datePickerMode=UIDatePickerModeDate;
    [alert addSubview:pickerView];
    [alert show];
}
Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
MAC113
  • 1,444
  • 12
  • 19

5 Answers5

1

iOS 7 doesn't support adding subviews to its hierarchy anymore. That's why I wrote SDCAlertView. It adds a contentView property which you can use to add any other view to the alert.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
0

In iOS7 UIAlertView's view hierarchy has been changed , so now you cant add any view to the UIAleartView

Dushyant Singh
  • 721
  • 4
  • 13
0

Adding subviews to a UIAlertView is not supported anymore, starting in iOS7. You can use third party view like: https://github.com/wimagguc/ios-custom-alertview

Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
0

Try this Code

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Hello"
     message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Stamp", nil];
    UIDatePicker  *pickerView=[[UIDatePicker alloc]initWithFrame:CGRectMake(0,40,246,150)];
    pickerView.datePickerMode=UIDatePickerModeDate;
     myAlertView.tag=101;
     [myAlertView show];
    UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, myAlertView.bounds.size.height, 320, 216)];
    // Add picker to alert
    [myAlertView addSubview:picker];
    // Adjust the alerts bounds
    myAlertView.bounds = CGRectMake(0, 0, 320 + 20, myAlertView.bounds.size.height + 216 + 20);
     [myAlertView release];
user3091160
  • 145
  • 2
  • 13
  • 1
    Won't work. `addSubview:` on `UIAlertView` will do absolutely nothing in iOS 7. – Scott Berrevoets Dec 11 '13 at 13:46
  • -1 as this is incorrect based on https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html you are not allowed to modify the `UIAlertView` hierarchy. – Popeye Dec 16 '13 at 13:39
0

Unfortunately Apple will not allow you to add subviews or modify the view hierarchy. See https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html for:

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

And see https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/UIAlertView.html#//apple_ref/doc/uid/TP40012857-UIAlertView for:

Appearance of Alert Views

You cannot customize the appearance of alert views.

Going against these will get your App rejected in the Apple Review process - so I would go back and look at your design of your app.

Community
  • 1
  • 1
Popeye
  • 11,839
  • 9
  • 58
  • 91