如何創建一個通用的彈出視圖控制器,該控制器可以被具有不同數據的多個視圖控制器調用。 我創建了一個帶有標籤和按鈕的popupviewcontroller類。 根據來自不同視圖控制器的呼叫,標籤和按鈕將具有不同的文本。 總之什麼是創建可以由多個viewcontrollers如何在swift iOS中創建通用的彈出視圖控制器
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()
}
});
}
}
,我這樣從另一個視圖控制器調用它使用一個通用的彈出觀點正確和可行的方法: -
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)
}
所以我想說它是通用的,說它是一個全球性的方法或從不同的視圖控制器調用同一類的東西
歡迎SO此功能。你會發現如果你添加你的代碼的例子,你通常會得到更好的答案。即使代碼寫得不好。你說你已經創建了一個班級,添加這個例子。 – BWMustang13