我正在寻找一种从多个视图控制器调用自定义警报视图的方法.到目前为止,我做了几次不同的尝试但没有成功.
>我创建了一个带有界面构建器的警报视图,它可以在一个视图控制器上正常工作
>然后我尝试以编程方式创建警报视图,认为它可能与未在其他视图控制器上连接的插座有关.这个也适用于一个视图控制器而不是另一个.
>我创建了一个单独的swift文件,并创建了一个公共函数和相同的结果.使用这最后一种方法,我能够在多个视图控制器上成功重用常规UIAlertController,但这并不是我想要的.
使用前两种方法,我没有得到任何编译错误.应用程序运行正常,然后当我从另一个视图控制器调用警报时崩溃.
提前感谢任何输入!
编辑:
当我输入另一个swift文件时,此示例有效.
public func showSimpleAlert(title: String, message: String?, presentingController: UIViewController) {
if IS_OS_8_OR_LATER() {
let controller = UIAlertController(title: title, message: message, preferredStyle: .Alert)
controller.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: { (action) -> Void in
}))
presentingController.presentViewController(controller, animated: true, completion: nil)
} else {
let alert = UIAlertView(title: title, message: message, delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
}
这是我想要工作的人.
public func showAlert(oMsg: String, oTitle:String) {
alertView.backgroundColor = UIColor.whiteColor()
alertView.layer.cornerRadius = 25
alertTitleLabel.text = oTitle as String
alertTitleLabel.font = UIFont(name: "Open-Sans-Bold", size: 20)
alertTitleLabel.textColor = UIColor.blackColor()
alertTitleLabel.textAlignment = .Center
alertTitleLabel.numberOfLines = 1
alertTitleLabel.frame = CGRectMake(25, 60, 264, 112)
alertLabel.text = oMsg as String
alertLabel.font = UIFont(name: "Open-Sans", size: 20)
alertLabel.textColor = UIColor.blackColor()
alertLabel.textAlignment = .Center
alertLabel.numberOfLines = 4
alertLabel.frame = CGRectMake(25, 130, 264, 112)
okButton.setTitle("OK", forState: .Normal)
okButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
okButton.frame = CGRectMake(60, 230, 197, 75)
okButton.addTarget(UIViewController.self, action:#selector(LoginViewController.buttonAction(_:)), forControlEvents: .TouchUpInside)
}
最佳答案 我将给出一个简单的自定义alertview的答案,它基本上是一个修改过的uiviewcontroller.您可以使用uiviewcontroller作为uialertviewcontroller,如下所示.
简单的AlertView ::
AlertVC:
import UIKit
class ErrorAlert: UIViewController {
var titlenote:String = ""
var message:String = ""
@IBOutlet weak var cancelBtn: UIButton!
@IBOutlet weak var messageHolder: UILabel!
@IBOutlet weak var imageHolder: UIImageView!
@IBOutlet weak var titleHolder: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.7)
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.messageHolder.text = self.message
self.titleHolder.text = self.titlenote
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func dismiss(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
}
这个viewcontroller可以在任何vc中重复使用多次.
用法示例::
let alertController = self.storyboard?.instantiateViewController(withIdentifier: "erroralert") as! ErrorAlert
alertController.titlenote = "Invalid login"
alertController.message = "Invalid facebook account."
alertController.providesPresentationContextTransitionStyle = true
alertController.definesPresentationContext = true
alertController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
alertController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
self.present(alertController, animated: true, completion: nil)
我通过设置alpha值来制作alertviewvc半透明的背景.
实际显示::
您可以通过此方法制作更复杂的警报视图,但为了可重用性,您已应用一些逻辑,因为不同的viewcontroller按钮操作将不同.示例 – 有时您可以使用alertview进行注销警报或某些时候提交表单.因此,在这两种情况下,操作都会有所不同,因此为了可重用性,您必须编写额外的逻辑.
另一个alertView ::
我希望我的回答能帮助你.:)