GVKun编程网logo

ios-无论视图层次如何,都在所有内容之上显示UIAlertController

26

对于ios-无论视图层次如何,都在所有内容之上显示UIAlertController感兴趣的读者,本文将提供您所需要的所有信息,并且为您提供关于iOS-UIAlertController三种显示提示框

对于ios-无论视图层次如何,都在所有内容之上显示UIAlertController感兴趣的读者,本文将提供您所需要的所有信息,并且为您提供关于iOS - UIAlertController三种显示提示框代码、ios UIAlertController、IOS UIAlertController 弹出框中添加视图(例如日期选择器等等)、iOS UIAlertController 弹出问题的宝贵知识。

本文目录一览:

ios-无论视图层次如何,都在所有内容之上显示UIAlertController

ios-无论视图层次如何,都在所有内容之上显示UIAlertController

我正在尝试提供一个帮助类,以呈现一个UIAlertController。由于它是一个帮助器类,因此我希望它能在不考虑视图层次结构的情况下正常工作,并且没有有关它的信息。我能够显示警报,但是当它被关闭时,该应用程序崩溃并显示:

*** Terminating app due to uncaught exception ''NSInternalInconsistencyException'',reason: ''Trying to dismiss UIAlertController <UIAlertController: 0x135d70d80> with unknown presenter.''

我用以下方法创建弹出窗口:

guard let window = UIApplication.shared.keyWindow else { return }let view = UIView()view.isUserInteractionEnabled = truewindow.insertSubview(view, at: 0)window.bringSubview(toFront: view)// add full screen constraints to view ...let controller = UIAlertController(  title: "confirm deletion?",  message: ":)",  preferredStyle: .alert)let deleteAction = UIAlertAction(  title: "yes",  style: .destructive,  handler: { _ in    DispatchQueue.main.async {      view.removeFromSuperview()      completion()    }  })controller.addAction(deleteAction)view.insertSubview(controller.view, at: 0)view.bringSubview(toFront: controller.view)// add centering constraints to controller.view ...

当我点击时yes,应用程序将崩溃,并且在崩溃之前未单击处理程序。我无法显示,UIAlertController因为这将取决于当前视图层次结构,而我希望弹出窗口是独立的

编辑:快速解决方案感谢@Vlad的想法。似乎在单独的窗口中操作要简单得多。所以这是一个可行的Swift解决方案:

class Popup {  private var alertWindow: UIWindow  static var shared = Popup()  init() {    alertWindow = UIWindow(frame: UIScreen.main.bounds)    alertWindow.rootViewController = UIViewController()    alertWindow.windowLevel = UIWindowLevelAlert + 1    alertWindow.makeKeyAndVisible()    alertWindow.isHidden = true  }  private func show(completion: @escaping ((Bool) -> Void)) {    let controller = UIAlertController(      title: "Want to do it?",      message: "message",      preferredStyle: .alert    )    let yesAction = UIAlertAction(      title: "Yes",      style: .default,      handler: { _ in        DispatchQueue.main.async {          self.alertWindow.isHidden = true          completion(true)        }    })    let noAction = UIAlertAction(      title: "Not now",      style: .destructive,      handler: { _ in        DispatchQueue.main.async {          self.alertWindow.isHidden = true          completion(false)        }    })    controller.addAction(noAction)    controller.addAction(yesAction)    self.alertWindow.isHidden = false    alertWindow.rootViewController?.present(controller, animated: false)  }}

答案1

小编典典

2019年12月16日更新:

只需显示当前最顶层视图控制器中的视图控制器/警报即可。那可行 :)

if #available(iOS 13.0, *) {     if var topController = UIApplication.shared.keyWindow?.rootViewController  {           while let presentedViewController = topController.presentedViewController {                 topController = presentedViewController                }     topController.present(self, animated: true, completion: nil)}

2019年7月23日更新:

重要

显然,此技术下的方法 已在iOS 13.0中停止工作 :(

我会在有时间调查时会更新…

旧技术:

这是它的Swift(5)扩展:

public extension UIAlertController {    func show() {        let win = UIWindow(frame: UIScreen.main.bounds)        let vc = UIViewController()        vc.view.backgroundColor = .clear        win.rootViewController = vc        win.windowLevel = UIWindow.Level.alert + 1  // Swift 3-4: UIWindowLevelAlert + 1        win.makeKeyAndVisible()            vc.present(self, animated: true, completion: nil)    }}

只需设置您的UIAlertController,然后调用:

alert.show()

不再受View Controllers层次结构的约束!

iOS - UIAlertController三种显示提示框代码

iOS - UIAlertController三种显示提示框代码

UIAlertView在IOS 8以上版本已经过时了,官方推荐我们使用UIAlertController代替UIAlertView、UIActionSheet

1、UIAlertController显示普通的Alert

 

 
- (IBAction)showAlert:(UIButton *)sender {
    //显示提示框
    //过时
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
//    [alert show];
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
                                                                   message:@"This is an alert."
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              //响应事件
                                                              NSLog(@"action = %@", action);
                                                          }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              //响应事件
                                                              NSLog(@"action = %@", action);
                                                          }];

    [alert addAction:defaultAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}

 

 

 

2、UIAlertController显示带文本输入的的Alert

 

- (IBAction)showList:(UIButton *)sender {
    //提示框添加文本输入框
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
                                                                   message:@"This is an alert."
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              //响应事件
                                                              //得到文本信息
                                                              for(UITextField *text in alert.textFields){
                                                                  NSLog(@"text = %@", text.text);
                                                              }
                                                          }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * action) {
                                                             //响应事件
                                                             NSLog(@"action = %@", alert.textFields);
                                                         }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"登录";
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"密码";
        textField.secureTextEntry = YES;
    }];
    
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];

}

 

 

 

3、UIAlertController显示ActionSheet

 

- (IBAction)showSheet:(UIButton *)sender {
    //显示弹出框列表选择
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
                                                                   message:@"This is an Sheet."
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                          handler:^(UIAlertAction * action) {
                                                              //响应事件
                                                              NSLog(@"action = %@", action);
                                                          }];
    UIAlertAction* deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive
                                                         handler:^(UIAlertAction * action) {
                                                             //响应事件
                                                             NSLog(@"action = %@", action);
                                                         }];
    UIAlertAction* saveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action) {
                                                             //响应事件
                                                             NSLog(@"action = %@", action);
                                                         }];
    [alert addAction:saveAction];
    [alert addAction:cancelAction];
    [alert addAction:deleteAction];
    [self presentViewController:alert animated:YES completion:nil];
}

 

ios UIAlertController

ios UIAlertController

高春辉、王春生、朱峰:关于开源创业的 15 件小事

提示框

- (void)login{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil                                                                             message: nil                                                                       preferredStyle:UIAlertControllerStyleActionSheet];
    //添加Button
    [alertController addAction: [UIAlertAction actionWithTitle: @"拍照" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        //处理点击拍照
    }]];
    [alertController addAction: [UIAlertAction actionWithTitle: @"从相册选取" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        //处理点击从相册选取
    }]];
    [alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController: alertController animated: YES completion: nil];
}

警告框

- (void)zhuce
{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"登陆"
                                                                              message: @"输入用户名密码"
                                                                       preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"name";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"password";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.secureTextEntry = YES;
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSArray * textfields = alertController.textFields;
        UITextField * namefield = textfields[0];
        UITextField * passwordfiled = textfields[1];
        NSLog(@"%@:%@",namefield.text,passwordfiled.text);
        
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}

使用的步骤

第一步 初始化

+ (instancetype)alertControllerWithTitle:(NSString *)title
                                 message:(NSString *)message
                          preferredStyle:(UIAlertControllerStyle)preferredStyle

 

这里的 preferredStyle 有两种,sheet 和 alert

typedef enum UIAlertControllerStyleNSInteger {
   UIAlertControllerStyleActionSheet = 0,
   UIAlertControllerStyleAlert 
} UIAlertControllerStyle;

 

第二步,添加 Action(button 或者 textfield)
添加 Button
- (void)addAction:(UIAlertAction *)action
这里的 UIAlertAction 是一个比较简单的类

+ (instancetype)actionWithTitle:(NSString *)title
                          style:(UIAlertActionStyle)style
                        handler:(void (^)(UIAlertAction *action))handler

 

style 有三种

typedef enum UIAlertActionStyleNSInteger {
   UIAlertActionStyleDefault = 0,//默认
   UIAlertActionStyleCancel,//取消
   UIAlertActionStyleDestructive //有可能改变或者数据UIAlertActionStyle;

 

添加 TextField
注意,只能是 UIAlertControllerStyleAlert 才能添加 Textfield

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler

 

在 block 里配置 textfield 的信息,例如 placeholder,backgroundcolor 等。
Textfield 的保存信息可由 UIAlertController 的属性 Textfields 获得。如同上述的例子二一样。

第三步,显示
例如

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


IOS UIAlertController 弹出框中添加视图(例如日期选择器等等)

IOS UIAlertController 弹出框中添加视图(例如日期选择器等等)

UIDatePicker *datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerModeDate;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n" message:nil   preferredStyle:UIAlertControllerStyleActionSheet];
[alert.view addSubview:datePicker];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
        //实例化一个NSDateFormatter对象
        [dateFormat setDateFormat:@"yyyy-MM-dd"];//设定时间格式
        NSString *dateString = [dateFormat stringFromDate:datePicker.date];
        //求出当天的时间字符串
        NSLog(@"%@",dateString);
    }];
 UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  }];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:^{ }];


需要注意的是,这里没有设置地区时间

    [datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];


iOS UIAlertController 弹出问题

iOS UIAlertController 弹出问题

xcode 7.1 下弹出 UIalertController ,  无法弹出,报  Attempt to present <UIAlertController: 0x7fe20bfe03b0> on <ViewController: 0x7fe20bfda980> whose view is not in the window hierarchy!
 警告,如果添加 UINavigationController 后,UIalertController 可以弹出, 但报 Presenting view controllers on detached view controllers is discouraged 警告,该如何解决

我们今天的关于ios-无论视图层次如何,都在所有内容之上显示UIAlertController的分享就到这里,谢谢您的阅读,如果想了解更多关于iOS - UIAlertController三种显示提示框代码、ios UIAlertController、IOS UIAlertController 弹出框中添加视图(例如日期选择器等等)、iOS UIAlertController 弹出问题的相关信息,可以在本站进行搜索。

本文标签: