如何创建可由具有不同数据的多个视图控制器调用的通用弹出视图控制器。 我创建了一个带有标签和按钮的popupviewcontroller类。 标签和按钮将根据来自不同视图控制器的调用而具有不同的文本。 简而言之,创建可供多个viewcontrollers使用的通用弹出视图的正确和可行方法
class CustomPopUpViewController: UIViewController{
@IBOutlet weak var vWCustomSubVw: UIView!
@IBOutlet weak var lblHeadingText: UILabel!
@IBOutlet weak var lblDescription: UILabel!
@IBOutlet weak var btnCustom: UIButton!
var strLblHeadingText = String() // string for heading label
var strLblDescription = String() // string for description label
var strBtnCustom = String()// string for custom button
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.2)
self.showAnimate()
lblHeadingText.text = strLblHeadingText
lblDescription.text = strLblDescription
btnCustom .setTitle(strBtnCustom, for: UIControlState.normal)
}
func showAnimate()
{
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0;
UIView.animate(withDuration: 0.25, animations: {
self.view.alpha = 1.0
self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
});
}
func removeAnimate()
{
UIView.animate(withDuration: 0.25, animations: {
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0;
}, completion:{(finished : Bool) in
if (finished)
{
self.view.removeFromSuperview()
}
});
}
}
我正在从另一个这样的viewcontroller中调用它: -
func btnInfoTapped(){
let customPopUpVC = UIStoryboard(name: "Course", bundle: nil).instantiateViewController(withIdentifier: "CustomPopUpViewController") as! CustomPopUpViewController
self.addChildViewController(customPopUpVC)
customPopUpVC.view.frame = self.view.frame
self.view.addSubview(customPopUpVC.view)
customPopUpVC.didMove(toParentViewController: self)
}
所以我想让它变得通用,说它是一个全局方法或者从不同的Viewcontrollers调用同一个类的东西
答案 0 :(得分:1)
您可以创建一个显示警报控制器的常用方法
import Foundation
import UIKit
class AlertHelper: NSObject {
//Alert with title and dismiss button only
static func showAlertWithTitle(_ conroller: UIViewController, title: String, message: String = "" ,dismissButtonTitle: String, dismissAction:@escaping ()->Void) {
let validationLinkAlert = UIAlertController(title:title, message:message, preferredStyle: .alert)
let dismissAction = UIAlertAction(title: dismissButtonTitle, style: .default) { (action) -> Void in
dismissAction()
}
validationLinkAlert.addAction(dismissAction)
conroller.present(validationLinkAlert, animated: true, completion: nil)
}
//Alert with title with message
static func showALertWithTitleAndMessage(_ controller: UIViewController, title: String, message: String, dismissButtonTitle: String, okButtonTitle: String, dismissAction:@escaping ()-> Void, okAction:@escaping ()-> Void) {
let validationLinkAlert = UIAlertController(title:title, message:message, preferredStyle: .alert)
let dismissAction = UIAlertAction(title: dismissButtonTitle, style: UIAlertActionStyle.default) { (action) in
dismissAction()
}
let okAction = UIAlertAction(title: okButtonTitle, style: UIAlertActionStyle.default) { (action) in
okAction()
}
validationLinkAlert.addAction(dismissAction)
validationLinkAlert.addAction(okAction)
controller.present(validationLinkAlert, animated: true, completion: nil)
}
}
从Viewcontroller中调用此函数
AlertHelper.showAlertWithTitle(self, title: message, dismissButtonTitle: "OK") { () -> Void in
}
答案 1 :(得分:0)
我认为这个pod会帮助你EzPopup(https://github.com/huynguyencong/EzPopup)。您可以轻松地将视图控制器显示为弹出窗口:
// init YourViewController
let contentVC = ...
// Init popup view controller with content is your content view controller
let popupVC = PopupViewController(contentController: contentVC, popupWidth: 100, popupHeight: 200)
// show it by call present(_ , animated:) method from a current UIViewController
present(popupVC, animated: true)