iOS 9 的 UIAlertView 的替代方案

objectiveIOS

1个回答

写回答

琪娧

2025-06-23 06:35

+ 关注

IOS
IOS

IOS 9 中,UIAlertView 被废弃,替代方案是使用 UIAlertController。UIAlertController 是一个更加灵活和强大的类,可以用于显示各种提示框和操作表。它提供了三种样式:UIAlertControllerStyleAlert、UIAlertControllerStyleActionSheet 和 UIAlertControllerStyleLogin。

UIAlertControllerStyleAlert

UIAlertControllerStyleAlert 是用于显示警告框的样式。警告框通常用于向用户展示重要信息或者需要确认的操作。下面是一个示例代码:

Swift

let alertController = UIAlertController(title: "提示", message: "确定要删除该文件吗?", preferredStyle: .alert)

let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (action) in

// 取消操作

}

let deleteAction = UIAlertAction(title: "删除", style: .destructive) { (action) in

// 删除操作

}

alertController.addAction(cancelAction)

alertController.addAction(deleteAction)

self.present(alertController, animated: true, completion: nil)

在上面的代码中,我们首先创建了一个 UIAlertController,设置了标题为 "提示",消息为 "确定要删除该文件吗?",样式为 .alert。然后我们创建了两个 UIAlertAction,一个是 "取消",一个是 "删除",并为它们分别设置了不同的样式。最后将这两个 UIAlertAction 添加到 UIAlertController 中,并通过 present(_:animated:completion:) 方法显示出来。

UIAlertControllerStyleActionSheet

UIAlertControllerStyleActionSheet 是用于显示操作表的样式。操作表通常用于提供多个选项供用户选择。下面是一个示例代码:

Swift

let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

let cameraAction = UIAlertAction(title: "拍照", style: .default) { (action) in

// 拍照操作

}

let albumAction = UIAlertAction(title: "从相册选择", style: .default) { (action) in

// 从相册选择操作

}

let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (action) in

// 取消操作

}

alertController.addAction(cameraAction)

alertController.addAction(albumAction)

alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion: nil)

在上面的代码中,我们创建了一个 UIAlertController,设置样式为 .actionSheet。然后创建了两个 UIAlertAction,一个是 "拍照",一个是 "从相册选择",并为它们设置了默认样式。最后创建了一个取消操作的 UIAlertAction,样式为 .cancel。将这三个 UIAlertAction 添加到 UIAlertController 中,并通过 present(_:animated:completion:) 方法显示出来。

UIAlertControllerStyleLogin

UIAlertControllerStyleLogin 是用于显示登录框的样式。登录框通常用于需要用户输入用户名和密码的场景。下面是一个示例代码:

Swift

let alertController = UIAlertController(title: "登录", message: nil, preferredStyle: .alert)

alertController.addTextField { (textField) in

textField.placeholder = "用户名"

}

alertController.addTextField { (textField) in

textField.placeholder = "密码"

textField.isSecureTextEntry = true

}

let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (action) in

// 取消操作

}

let loginAction = UIAlertAction(title: "登录", style: .default) { (action) in

// 登录操作

if let usernameTextField = alertController.textFields?.first, let passwordTextField = alertController.textFields?.last {

let username = usernameTextField.text ?? ""

let password = passwordTextField.text ?? ""

// 进行登录验证

}

}

alertController.addAction(cancelAction)

alertController.addAction(loginAction)

self.present(alertController, animated: true, completion: nil)

在上面的代码中,我们创建了一个 UIAlertController,设置标题为 "登录",样式为 .alert。然后通过 addTextField(configurationHandler:) 方法添加了两个文本输入框,分别用于输入用户名和密码。接着创建了一个取消操作的 UIAlertAction 和一个登录操作的 UIAlertAction,并将它们添加到 UIAlertController 中。在登录操作的闭包中,我们可以通过 alertController.textFields 获取到文本输入框,从而获取到用户输入的用户名和密码。

IOS 9 中,UIAlertController 是 UIAlertView 的替代方案。它提供了三种样式:UIAlertControllerStyleAlert、UIAlertControllerStyleActionSheet 和 UIAlertControllerStyleLogin,分别用于显示警告框、操作表和登录框。通过添加 UIAlertAction 和文本输入框,我们可以实现各种不同的提示框和操作表。这些新的 API 提供了更加灵活和强大的功能,使我们能够更好地满足用户的需求。

举报有用(4分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号