当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - UIAlertController is not is window hierarchy

问题描述:

Im trying to show up a pop up containing a UIPickerView and UITextField.

Following is my code that I use to create the pop up.

self.alert = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleAlert];

CGRect pickerFrame = CGRectMake(0, 0, 270, 200);

UIPickerView *regionsPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];

regionsPicker.tag = 1;

regionsPicker.dataSource = (id <UIPickerViewDataSource>)self;

regionsPicker.delegate = (id <UIPickerViewDelegate>) self;

[regionsPicker setShowsSelectionIndicator:YES];

UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 44.f)];

toolView.backgroundColor = [UIColor blackColor];

CGRect buttonFrame = CGRectMake(0, 5, 100, 30);

UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];

button.imageEdgeInsets = UIEdgeInsetsMake(0, button.titleLabel.frame.size.width, 0, -button.titleLabel.frame.size.width);

UIImage *btnImage = [UIImage imageNamed:@"delete.png"];

[button setImage:btnImage forState:UIControlStateNormal];

[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

[button setTitleColor: [UIColor blueColor] forState: UIControlStateNormal];

[toolView addSubview:button];

[button addTarget:self action:@selector(closeDialog:) forControlEvents:UIControlEventTouchUpInside];

self.textField = [[UITextView alloc] initWithFrame:CGRectMake(30, 170, 220, 200)];

_textField.editable = NO;

_textField.hidden=true;

self.textField.text = @"Enter text...";

self.textField.font = [UIFont systemFontOfSize:15];

self.textField.autocorrectionType = UITextAutocorrectionTypeNo;

self.textField.keyboardType = UIKeyboardTypeDefault;

self.textField.returnKeyType = UIReturnKeyDone;

self.textField.textColor = [UIColor lightGrayColor];

self.textField.delegate = (id <UITextViewDelegate>) self;

self.textField.layer.borderWidth = 1.0f;

self.textField.layer.cornerRadius = 5;

self.textField.layer.borderColor = [[UIColor grayColor] CGColor];

[toolView addSubview:self.textField];

CGRect actionFrame = CGRectMake(80, 400, 100, 30);

UIButton *actionBtn = [[UIButton alloc] initWithFrame: actionFrame];

[actionBtn setTitle:@"Submit" forState:UIControlStateNormal];

[actionBtn setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];

[actionBtn setBackgroundColor:[UIColor purpleColor]];

[self.textField setUserInteractionEnabled:YES];

[toolView setUserInteractionEnabled:YES];

[self.alert.view setUserInteractionEnabled:YES];

[self.alert.view addSubview:regionsPicker];

[self.alert.view addSubview:self.textField];

[self.alert.view addSubview:toolView];

[self.alert.view addSubview:actionBtn];

id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;

if([rootViewController isKindOfClass:[UINavigationController class]])

{

rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];

}

[rootViewController presentViewController:_alert animated:YES completion:nil];

It works fine.However, if Im trying to enter text in the UITextField by tapping it, the keyboard doesn't appear and hence Im not be able to type anything within the UITextField. How can I sort this out?

网友答案:

the reason is you added textField, button, all those inside toolview , but the toolview height is very less

change your toolView height from 44 into 500 or else (combination of textfield, button + 50). Change below statement

UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 44.f)]; 

to

 UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 500.f)];

Update

change this line

    [self presentViewController:alert animated:YES completion:nil];

into

   id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
  rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}
[rootViewController presentViewController:alert animated:YES completion:nil];

Update New

  UIViewController *rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
  rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}else
{
   while (rootViewController.presentedViewController) {
    rootViewController = rootViewController.presentedViewController;
}
 }
[rootViewController presentViewController:alert animated:YES completion:nil];

Swift

 var rootViewController: UIViewController = UIApplication.sharedApplication().delegate.window.rootViewController
if (rootViewController is UINavigationController.self) {
rootViewController = ((rootViewController as! UINavigationController)).viewControllers[0]
}
else {
while rootViewController.presentedViewController {
    rootViewController = rootViewController.presentedViewController
}
}
rootViewController.presentViewController(alert, animated: true, completion: { _ in })
网友答案:

Try this it may help you:-

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@" Your message " preferredStyle:UIAlertControllerStyleAlert];

UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 200)];
toolView.backgroundColor = [UIColor blackColor];

CGRect buttonFrame = CGRectMake(0, 5, 100, 30);
UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
[button setTitle:@"Image" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[toolView addSubview:button];

UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 270, 44)];
txtField.borderStyle = UITextBorderStyleRoundedRect;
txtField.backgroundColor = [UIColor grayColor];
txtField.font = [UIFont systemFontOfSize:15];
txtField.placeholder = @"enter text";
txtField.autocorrectionType = UITextAutocorrectionTypeNo;
txtField.keyboardType = UIKeyboardTypeDefault;
txtField.returnKeyType = UIReturnKeyDone;
txtField.clearButtonMode = UITextFieldViewModeWhileEditing;
txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txtField.delegate = (id<UITextFieldDelegate>)self;

[toolView addSubview:txtField];

[txtField setUserInteractionEnabled:YES];
[toolView setUserInteractionEnabled:YES];
[alertController.view setUserInteractionEnabled:YES];

[alertController.view addSubview:toolView];
[self presentViewController:alertController animated:YES completion:nil];
分享给朋友:
您可能感兴趣的文章:
随机阅读: