我正在寻找一种从多个视图控制器调用自定义警报视图的方法。到目前为止,我已经进行了几次不同的尝试,但均未成功。
我使用界面构建器创建了一个警报视图,该界面构建器在一个视图控制器上工作正常,而在另一个视图控制器上却无法正常工作。
然后,我尝试以编程方式创建警报视图,认为它可能与其他视图控制器上未连接插座有关。这个也使用一个视图控制器,而不是另一个。
我制作了一个单独的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的背景为半透明的。
实际显示::
您可以通过这种方法制作更复杂的alertview,但是对于可重用性,您需要应用一些逻辑,因为对于不同的viewcontroller,按钮动作将有所不同。示例-有时您可以使用alertview来注销警报,也可以某个时间来提交表单。因此在两种情况下,操作都将有所不同,因此为了可重用性,您必须编写额外的逻辑。
另一个alertView ::
希望我的回答对您有所帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句