Ios 如何在全球swift中创建uialertcontroller
我试图在Ios 如何在全球swift中创建uialertcontroller,ios,swift,uialertcontroller,Ios,Swift,Uialertcontroller,我试图在Config.swift文件中创建uialertcontroller,如下所示 static func showAlertMessage(titleStr:String, messageStr:String) -> Void { let window : UIWindow? let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertContro
Config.swift
文件中创建uialertcontroller
,如下所示
static func showAlertMessage(titleStr:String, messageStr:String) -> Void {
let window : UIWindow?
let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert);
self.window!.presentViewController(alert, animated: true, completion: nil)
}
问题是我在self.window!中发现了问题代码>
类型“Config”没有成员“window”
请让我知道如何解决这个问题。self.window
意味着这个类中有一个window
对象,但事实并非如此
您需要将let window:ui window?
与window?一起使用。presentViewController(警报、动画:true、完成:nil)
,但这不会有帮助,因为此窗口实际上并不表示任何现有窗口,而且它也不是视图控制器
因此,我建议您将要使用的实际视图控制器传递给该方法:
static func showAlertMessage(vc: UIViewController, titleStr:String, messageStr:String) -> Void {
let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert);
vc.presentViewController(alert, animated: true, completion: nil)
}
您可以从UIViewController对象可用的类中调用它。我建议您编写此代码,但如果确实需要,请尝试以下操作:
static func showAlertMessage(titleStr:String, messageStr:String) -> Void {
let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert);
if let viewController = UIApplication.sharedApplication().windows.first?.rootViewController as UIViewController? {
viewController.presentViewController(alert, animated: true, completion: nil)
}
}
至少它不会坏
@Eric最好回答。如果您想从AppDelegate窗口演示,可以这样使用
UIApplication.sharedApplication().delegate?.window.rootViewController?.presentViewController(vc, animated: true, completion: nil)
我建议创建一个扩展:
extension UIViewController {
func showAlertMessage(titleStr:String, messageStr:String) {
let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
}
}
我创建了一个alerMessage类。我可以调用应用程序中的任何位置
//Common Alert Message Class
class AlertMessage {
internal static var alertMessageController:UIAlertController!
internal static func disPlayAlertMessage(titleMessage:String, alertMsg:String){
AlertMessage.alertMessageController = UIAlertController(title: titleMessage, message:
alertMsg, preferredStyle: UIAlertControllerStyle.Alert)
AlertMessage.alertMessageController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil))
if let controller = UIApplication.sharedApplication().keyWindow?.rootViewController?.presentedViewController {
controller.presentViewController(AlertMessage.alertMessageController, animated: true, completion: nil)
}
else{
UIApplication.sharedApplication().delegate?.window!!.rootViewController?.presentViewController(AlertMessage.alertMessageController, animated: true, completion: nil)
}
return
}
}
swift
@IBAction func showDefaultAlert(_ sender: Any) {
Alert.showAlert(title:"Alert", message:"Default Alert")
}
这是我使用的,这与@penatheboss回答的相同,只是添加了添加操作和处理程序的能力
extension UIViewController {
func popupAlert(title: String?, message: String?, actionTitles:[String?], actions:[((UIAlertAction) -> Void)?]) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
for (index, title) in actionTitles.enumerated() {
let action = UIAlertAction(title: title, style: .default, handler: actions[index])
alert.addAction(action)
}
self.present(alert, animated: true, completion: nil)
}
}
只需确保actionTitles
和actions
数组的计数相同即可。如果不需要任何操作处理程序闭包,则传递nil
self.popupAlert(title: "Title", message: " Oops, xxxx ", actionTitles: ["Option1","Option2","Option3"], actions:[{action1 in
},{action2 in
}, nil])
目标C:
添加UIViewController
UIViewController+PopAlert.h
#import <UIKit/UIKit.h>
@interface UIViewController (UIViewControllerCategory)
- (void) popAlertWithTitle: (NSString*) title message: (NSString*) message actionTitles:(NSArray *) actionTitles actions:(NSArray*)actions;
@end
用法:
#import UIViewController+PopAlert.h
请参考下面的GIT示例
通用UIAlertViewController实现
import UIKit
protocol AlertViewControllerDelegate {
func SubmitAlertViewResult(textValue : String)
}
class AlertViewController {
static let sharedInstance = AlertViewController()
private init(){}
var delegate : AlertViewControllerDelegate?
func SubmitAlertView(viewController : UIViewController,title : String, message : String){
let alert = UIAlertController(title: title,
message: message,
preferredStyle: .alert)
// Submit button
let submitAction = UIAlertAction(title: "Submit", style: .default, handler: { (action) -> Void in
// Get 1st TextField's text
let textField = alert.textFields![0]
if(textField.text != "")
{
self.delegate?.SubmitAlertViewResult(textValue: textField.text!)
}
})
// Cancel button
let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { (action) -> Void in })
// Add 1 textField and cutomize it
alert.addTextField { (textField: UITextField) in
textField.keyboardAppearance = .dark
textField.keyboardType = .default
textField.autocorrectionType = .default
textField.placeholder = "enter any text value"
textField.clearButtonMode = .whileEditing
}
// Add action buttons and present the Alert
alert.addAction(submitAction)
alert.addAction(cancel)
viewController.present(alert, animated: true, completion: nil)
}
}
您可以尝试此操作,请在AppDelegate.swift
文件中添加以下代码
static func showAlertView(vc : UIViewController, titleString : String , messageString: String) ->()
{
let alertView = UIAlertController(title: titleString, message: messageString, preferredStyle: .alert)
let alertAction = UIAlertAction(title: "ok", style: .cancel) { (alert) in
vc.dismiss(animated: true, completion: nil)
}
alertView.addAction(alertAction)
vc.present(alertView, animated: true, completion: nil)
}
并从任何viewcontroller调用showAlertView方法
AppDelegate.showAlertView(vc: self, titleString: "testTitle", messageString: "test msg")
我使用的是@William Hu的解决方案:
func popup(caller:UIViewController, style:UIAlertControllerStyle? = UIAlertControllerStyle.alert,
title:String, message:String, buttonTexts:[String], buttonStyles:([UIAlertActionStyle?])? = nil,
handlers:[((UIAlertAction) -> Void)?], animated:Bool? = nil, completion: (() -> Void)? = nil) {
let alert = UIAlertController(title: title, message: message, preferredStyle: style!)
for i in 0..<buttonTexts.count {
alert.addAction(UIAlertAction(title: buttonTexts[i],
style: (buttonStyles == nil || i >= buttonStyles!.count || buttonStyles![i] == nil ?
UIAlertActionStyle.default : buttonStyles![i]!),
handler: (i >= handlers.count || handlers[i] == nil ? nil : handlers[i]!)))
}
caller.present(alert, animated: animated != nil ? animated! : true, completion: completion)
}
您可以使用为Swift4中的Show Alert创建的my实用程序类。只需编写一行代码即可使用:
显示简单警报
@IBAction func showDefaultAlert(_ sender: Any) {
Alert.showAlert(title:"Alert", message:"Default Alert")
}
演示代码链接:这也是您可以在可用的视图控制器顶部显示的方式
ui应用程序.topViewController()?.present(alertViewController!,动画:true,完成:nil)
详细信息
- Swift 5.1,Xcode 11.3.1
具有UIViewController扩展的全局UIAlertController
用法
警觉的
行动单
我将添加另一个类型为view controller的参数,并使用该控制器显示警报。您还应请求presenter作为参数。更新Xcode。清理你的项目。检查UIKit是否已导入。非常好的解决方案!将其放置在Globals.swift中,我可以从任何视图控制器调用它+1谢谢,我也添加了可编辑的动作样式,func-popupAlert(title:String?、message:String?、actionTitles:[String?]、actionStyles:[Int?]、actions:[((UIAlertAction)->Void)]{}
self.popupAlert(title:“title”、message:“Oops,xxxx”、actionTitles:[“Option1”、“Option2”、“Option3”]、actionsStyles:[UIAlertActionStyle.destructive.rawValue,UIAlertActionStyle.default.rawValue,,UIAlertActionStyle.default.rawValue]
…很好的解决方案,并质疑如何更改样式?@Jul2791您的意思是UIAlertAction(标题:标题,样式:.default
或preferredStyle:.alert
?然后只需将样式从.alert
更改为actionSheet
;与此相同,您可以将UIAlertAction
样式从更改为其他选项。@Uma您可以按类别或子类UIViewController
进行设置,就像我的编辑一样,我只为您提供分类方式。希望有帮助。请同时添加这一行…alert.addAction(UIAlertAction(标题:“Ok”,样式:。取消,处理程序:nil))@kishan94,这取决于程序员选择她/他想对警报做什么
func popup(caller:UIViewController, style:UIAlertControllerStyle? = UIAlertControllerStyle.alert,
title:String, message:String, buttonTexts:[String], buttonStyles:([UIAlertActionStyle?])? = nil,
handlers:[((UIAlertAction) -> Void)?], animated:Bool? = nil, completion: (() -> Void)? = nil) {
let alert = UIAlertController(title: title, message: message, preferredStyle: style!)
for i in 0..<buttonTexts.count {
alert.addAction(UIAlertAction(title: buttonTexts[i],
style: (buttonStyles == nil || i >= buttonStyles!.count || buttonStyles![i] == nil ?
UIAlertActionStyle.default : buttonStyles![i]!),
handler: (i >= handlers.count || handlers[i] == nil ? nil : handlers[i]!)))
}
caller.present(alert, animated: animated != nil ? animated! : true, completion: completion)
}
popup(caller: self, style: UIAlertControllerStyle.alert,
title: "Title", message: "Message",
buttonTexts: ["Destructive", "Cancel", "OK"],
buttonStyles: [UIAlertActionStyle.destructive, UIAlertActionStyle.cancel],
handlers: [nil], animated: false)
@IBAction func showDefaultAlert(_ sender: Any) {
Alert.showAlert(title:"Alert", message:"Default Alert")
}
extension UIViewController{
// Global Alert
// Define Your number of buttons, styles and completion
public func openAlert(title: String,
message: String,
alertStyle:UIAlertController.Style,
actionTitles:[String],
actionStyles:[UIAlertAction.Style],
actions: [((UIAlertAction) -> Void)]){
let alertController = UIAlertController(title: title, message: message, preferredStyle: alertStyle)
for(index, indexTitle) in actionTitles.enumerated(){
let action = UIAlertAction(title: indexTitle, style: actionStyles[index], handler: actions[index])
alertController.addAction(action)
}
self.present(alertController, animated: true)
}
}
self.openAlert(title: "alert",
message: "add your message",
alertStyle: .alert,
actionTitles: ["Okay", "Cancel"],
actionStyles: [.default, .cancel],
actions: [
{_ in
print("okay click")
},
{_ in
print("cancel click")
}
])
self.openAlert(title: "actionsheet",
message: "add your message",
alertStyle: .actionSheet,
actionTitles: ["Okay", "Cancel"],
actionStyles: [.default, .cancel],
actions: [
{_ in
print("okay click")
},
{_ in
print("cancel click")
}
])