2016-05-04 68 views
3

我试图显示弹出包含UIPickerViewUITextFieldUIAlertController不是窗口层次结构

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

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文字,键盘没有出现,因此我不是能够键入UITextField内任何东西。我怎样才能解决这个问题?

+0

问:为什么你的'textField'高度' 200'时,字体大小只是'15.0f'?还显示代码在哪里实现'UITextFieldDelegate'方法。 – rptwsthi

+2

我不认为你应该将子视图添加到“UIAlertController”,而是使用定制版本的“UIAlertController”来代替。 – Larme

+0

是否可以通过任何方式将视图或视图控制器显示为弹出窗口? – Roger

回答

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

负面选民可以解释downvote的原因 –

+0

最新的问题,如果你需要澄清肯定我会解释, –

+0

好的谢谢... – Roger

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]; 
相关问题