UIAlertController不是窗口层次结构

时间:2016-05-04 07:51:58

标签: ios objective-c swift uitextfield uialertcontroller

我正在尝试显示包含UIPickerViewUITextField

的弹出窗口

以下是我用来创建弹出窗口的代码。

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

它工作正常。但是,如果我试图通过点击它来在UITextField中输入文字,键盘就不会出现,因此我无法在UITextField中输入任何内容。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

  

原因是您添加了Uniform `GLUP`is not linkable between attached shaders textField,所有这些都在工具视图中,但button高度非常低

从44更改toolView高度 到500或者其他(textfield,button + 50的组合)。 更改以下声明

toolview

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

<强>更新

更改此行

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

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

更新新

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

<强>夫特

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

答案 1 :(得分:1)

试试这可能会对你有所帮助: -

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