【问题标题】:UIAlertController is not is window hierarchyUIAlertController 不是窗口层次结构
【发布时间】:2016-05-04 07:51:57
【问题描述】:

我试图显示一个包含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 中输入任何内容。我该如何解决这个问题?

【问题讨论】:

  • 问题:为什么你的textField的高度是200,而字体大小只有15.0f?还显示您在哪里实现UITextFieldDelegate 方法的代码。
  • 我认为您不应该将子视图添加到 UIAlertController,而应该使用自定义版本的“UIAlertController”。
  • 是否可以通过任何方式将视图或视图控制器显示为弹出窗口?

标签: ios objective-c swift uitextfield uialertcontroller


【解决方案1】:

原因是你添加了textFieldbutton,都是toolview里面的,但是toolview的高度太小了

将您的 toolView 高度从 44 更改 进入 500 或其他(文本字段、按钮 + 50 的组合)。 更改以下语句

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

斯威夫特

 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 })

【讨论】:

  • 反对票的人可以解释下票的原因
  • 有什么问题,如果您需要澄清,我一定会解释,
  • @user22312412 - 简单的兄弟,你在哪里贴代码
  • 别这么想,你离得更近了,如果你修改一些东西效果更好,
  • @KaruppuMGR - 很高兴听到兄弟,然后根据设备宽度检查并设置您的框架(x、y、宽度和高度),无论如何,如果我的回答有用,请接受它在未来很有用
【解决方案2】:

试试这个可能对你有帮助:-

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-06
    • 2023-03-31
    • 2014-11-17
    • 2018-11-14
    • 1970-01-01
    • 2020-08-07
    • 2017-07-27
    • 2015-05-29
    相关资源
    最近更新 更多