我们如何可以像在 UIAlertViewUIView上UIAlertController一样添加on ,@"AccessoryView"但现在它在 iOS 8 中已弃用。所以我想用它,UIAlertController但是怎么用呢?
我有一个UIController的操作表类型,显示两个项目,第一个是"从App退出",另一个是"取消".我想将显示"从应用程序注销"的按钮的字体颜色更改为红色,并将"取消"按钮保留为默认颜色.我怎样才能做到这一点?以下是我的代码:
let cancelActionSheetButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
(cancelSelected) -> Void in
print("Cancel Selected")
}
let logoutActionSheet = UIAlertController(title:"Logout", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let logoutActionSheetButton = UIAlertAction(title: "Logout from App", style: UIAlertActionStyle.Default) {
(logoutSelected) -> Void in
}
logoutActionSheet.addAction(logoutActionSheetButton)
logoutActionSheet.addAction(cancelActionSheetButton)
self.presentViewController(logoutActionSheet, animated: true, completion:nil)
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有表视图控制器.当用户在tableview的最后一行键入时,应显示操作表以要求注销.以下是此操作的代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 0:
//..
case 1:
//..
case 2:
//..
case 3:
let logOutMenu = UIAlertController(title: nil, message: "Are you sure want to logout?", preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let logOutAction = UIAlertAction(title: "Log out", style: .default, handler: { (UIAlertAction) in
print("sign out")
})
logOutMenu.addAction(cancelAction)
logOutMenu.addAction(logOutAction)
self.present(logOutMenu, animated: true, completion: nil)
default: break
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常,但行动表有奇怪的行为.显示操作表大约需要10秒钟(甚至更长时间).我在真实设备上也注意到了同样的行为.我做错了什么?
我创建了一个覆盖来运行,同时我向服务器运行异步数据抓取,这样用户就不会继续按下UI中的按钮,直到数据抓取完成.我已经将函数放入一个全局单例类中,并在传入bool时调用它来说明我是否要显示或隐藏.我可以让它显示,但我不能让它隐藏.这是代码:
class DataModel {
static let sharedInstance = DataModel()
func accessNetworkData(vc: UIViewController, params: [String:Any], wsURLPath: String, completion: @escaping (_ response: AnyObject) -> ()) {
DataModel.sharedInstance.toggleModalProgess(show: true)
// SHOW THE MODAL HERE ONCE THE DATA IS REQUESTED.
let url = URL(string: wsURLPath)!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
do { request.httpBody = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted) } catch let error { print(error.localizedDescription) }
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTask(with: request …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个带有两个输入字段的警报,其中包含此应用程序的主密码。这是我的第一个应用程序。我在网上看到一个函数,想看看它是否仍然有效,但调用该函数时似乎没有弹出警报。Swift 4 中对此进行了更改吗?
func showInputDialog() {
//Creating UIAlertController and
//Setting title and message for the alert dialog
let alertController = UIAlertController(title: "Choose Master Password", message: "Enter your Master and confirm it!", preferredStyle: .alert)
//the confirm action taking the inputs
let confirmAction = UIAlertAction(title: "Enter", style: .default) { (_) in
//getting the input values from user
let master = alertController.textFields?[0].text
let confirm = alertController.textFields?[1].text
if master == confirm {
self.labelCorrect.isHidden = true
self.labelCorrect.text = master
}
}
//the cancel action doing …Run Code Online (Sandbox Code Playgroud) *** 由于未捕获的异常“NSObjectNotAvailableException”而终止应用程序,原因:“UIAlertView 已弃用且不可用于基于 UIScene 的应用程序,请使用 UIAlertController!”
从 WKWebView 发出警报时,它会引发上述错误。
UIAlertView 是从网页中的 Javascript 调用的。我没有手动调用 UIAlertView。
import UIKit
import WebKit
class ViewController: UIViewController{
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.google.com/maps")!
webView.load(URLRequest(url: url))
}
}
Run Code Online (Sandbox Code Playgroud)
当我单击“您的位置”时加载地图后,应用程序崩溃。
所以我想要一个警告弹出告诉我一条消息,然后我希望它等我,直到我按OK然后它将继续该功能.例如.
@IBAction Alert() {
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
问题是,现在它不会做任何事情,如果我之后只是放入一个函数它直接执行,而不是等到我按下OK.有谁知道如何做到这一点?
顺便说一下,在这个例子中,消息将是标题,消息,我知道,这不是我在这种情况下需要的;)
提前致谢!
我目前正在使用以下代码(Swift)将图像添加到UIAlertAction。
purpleAction.setValue(pCircle, forKey: "image")
如果可能,我想在UIAlertAction中将图像居中。当前,UIAlertAction的标题位于中间,图像显示在左侧。
这是我的应用程序的简历:
代码在方法中tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool.
self.selectedIndex = #,VC会显示,但没有动画.有没有办法实现相同的动作,如点击项目?谢谢我不想更改UILabel.appearance,因为它将应用于所有标签。
我如何才能显示UIAlertController如下图所示?
需要在第二个位置显示粗体按钮。

默认情况下,当我进行设置时,UIAlertActionStyle.cancel它会显示在第一个位置的“确认”按钮上。
现在看起来像这样:

我做了一些研究,但没有找到任何解决方案。