首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIAlertController不是窗口层次结构

UIAlertController不是窗口层次结构
EN

Stack Overflow用户
提问于 2016-05-04 07:51:57
回答 2查看 190关注 0票数 1

我试图显示一个包含UIPickerViewUITextField的弹出。

下面是我用来创建弹出的代码。

代码语言:javascript
运行
复制
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];

它工作fine.However,如果我试图通过点击输入UITextField中的文本,键盘就不会出现,因此Im无法在UITextField中键入任何内容。我怎么才能解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-04 08:35:21

原因是您添加了textFieldbutton和所有这些工具视图,但是toolview高度非常低。

将toolView高度从44更改为500或其他( textfield,按钮+50的组合)。更改以下报表

代码语言:javascript
运行
复制
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 44.f)]; 

代码语言:javascript
运行
复制
 UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 500.f)];

更新

更改这一行

代码语言:javascript
运行
复制
    [self presentViewController:alert animated:YES completion:nil];

转到

代码语言:javascript
运行
复制
   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];

更新新

代码语言:javascript
运行
复制
  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

代码语言:javascript
运行
复制
 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 })
票数 2
EN

Stack Overflow用户

发布于 2016-05-04 11:24:04

试试这个也许能帮到你:-

代码语言:javascript
运行
复制
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];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37021648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档