Ios UIAlertController不是窗口层次结构

Ios UIAlertController不是窗口层次结构,ios,objective-c,swift,uitextfield,uialertcontroller,Ios,Objective C,Swift,Uitextfield,Uialertcontroller,我试图显示一个弹出窗口,其中包含UIPickerView和UITextField 下面是我用来创建弹出窗口的代码 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

我试图显示一个弹出窗口,其中包含
UIPickerView
UITextField

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

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];
self.alert=[UIAlertController alertControllerWithTitle:nil消息:@“\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n“首选样式:UIAlertControllerStyleAlert];
CGRect pickerFrame=CGRectMake(0,0270200);
UIPickerView*RegionPicker=[[UIPickerView alloc]initWithFrame:pickerFrame];
regionsPicker.tag=1;
regionsPicker.dataSource=(id)self;
regionsPicker.delegate=(id)self;
[区域选择器设置显示选择指示器:是];
UIView*toolView=[[UIView alloc]initWithFrame:CGRectMake(0,0270.0f,44.f)];
toolView.backgroundColor=[UIColor blackColor];
CGRect buttonFrame=CGRectMake(0,5100,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 ImageName:@“delete.png”];
[按钮设置图像:btnImage for状态:uicontrol状态正常];
[按钮设置标题颜色:[UIColor whiteColor]用于状态:UIControlStateNormal];
[按钮设置标题颜色:[UIColor blueColor]用于状态:uicontrol状态正常];
[工具视图添加子视图:按钮];
[按钮添加目标:自我操作:@selector(closeDialog:)forControlEvents:UIControlEventTouchUpInside];
self.textField=[[UITextView alloc]initWithFrame:CGRectMake(30170220200)];
_textField.editable=否;
_textField.hidden=true;
self.textField.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)self;
self.textField.layer.borderWidth=1.0f;
self.textField.layer.cornerRadius=5;
self.textField.layer.borderColor=[[UIColor grayColor]CGColor];
[工具视图添加子视图:self.textField];
CGRect actionFrame=CGRectMake(8040010030);
UIButton*actionBtn=[[UIButton alloc]initWithFrame:actionFrame];
[actionBtn setTitle:@“提交”状态:UIControlStateNormal];
[actionBtn setTitleColor:[UIColor whiteColor]用于状态:UIControlStateNormal];
[actionBtn setBackgroundColor:[UIColor purpleColor];
[self.textField setUserInteractionEnabled:是];
[toolView setUserInteractionEnabled:是];
[self.alert.view setUserInteractionEnabled:是];
[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是类的种类:[UINavigationController类]])
{
rootViewController=[((UINavigationController*)rootViewController.ViewController对象索引:0];
}
[rootViewController presentViewController:_警报动画:是完成:无];
它工作正常。但是,如果我试图通过点击在
UITextField
中输入文本,键盘不会出现,因此我无法在
UITextField
中键入任何内容。我怎样才能解决这个问题

原因是您添加了
文本字段
按钮
,所有这些都在工具视图中,但是
工具视图
的高度非常低

将“工具视图”高度从44更改为44 输入500或其他(文本字段的组合,按钮+50)。 更改以下声明

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

更新

换行

    [self 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];
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 })

试试这个,它可能会帮助你:-

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];
UIAlertController*alertController=[UIAlertController alertControllerWithTitle:nil消息:@“您的消息”首选样式:UIAlertControllerStyleAlert];
UIView*toolView=[[UIView alloc]initWithFrame:CGRectMake(0,0,270,200)];
toolView.backgroundColor=[UIColor blackColor];
CGRect buttonFrame=CGRectMake(0,5100,30);
UIButton*button=[[UIButton alloc]initWithFrame:buttonFrame];
[按钮设置标题:@“图像”用于状态:uicontrol状态正常];
[按钮设置标题颜色:[UIColor blueColor]用于状态:uicontrol状态正常];
[工具视图添加子视图:按钮];
UITextField*txtField=[[UITextField alloc]initWithFrame:CGRectMake(0,0,270,44)];
txtField.borderStyle=UITextBorderStyleRoundedRect;
txtField.backgroundColor=[UIColor grayColor];
txtField.font=[UIFont systemFontOfSize:15];
txtField.placeholder=@“输入文本”;
txtField.autocorrectionType=UITextAutocorrectionTypeNo;
txtField.keyboardType=UIKeyboardTypeDefault;
txtField.returnKeyType=UIReturnKeyDone;
txtField.clearButtonMode=UITextFieldViewModeWhileEditing;
txtField.contentVerticalAlignment=uicontrol contentVerticalAlignment中心;
txtField.delegate=(id)self;
[工具视图添加子视图:txtField];
[txtField setUserInteractionEnabled:是];
[toolView setUserInteractionEnabled:是];
[alertController.view setUserInteractionEnabled:是];
[alertController.view添加子视图:工具视图];
[self-presentViewController:alertController动画:是完成:无];

问题:当字体大小仅为
15.0f
时,为什么您的
textField
高度为
200
?还显示要在何处实现
UITextFieldDelegate
方法的代码。我认为不应将子视图添加到
UIAlertController
中,而应使用自定义版本的“UIAlertController”相反,我是否可以通过任何方式将视图或视图控制器显示为一个弹出窗口?负面投票人可以解释向下投票的原因问题是什么,如果你需要澄清,我当然会解释,@user22312412-简单兄弟,好的,你在哪里贴了代码
 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 })
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];