gpt4 book ai didi

ios - 如何在 swift iOS 中创建一个通用的弹出 View Controller

转载 作者:行者123 更新时间:2023-11-28 06:21:49 24 4
gpt4 key购买 nike

如何创建可由具有不同数据的多个 View Controller 调用的通用弹出 View Controller 。我创建了一个带有标签和按钮的 popupviewcontroller 类。根据来自不同 View Controller 的调用,标签和按钮将具有不同的文本。简而言之,创建可由多个 View Controller 使用的通用弹出 View 的正确可行方法是什么

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()
}
});
}


}

我正在从另一个 View Controller 调用它:-

 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)



}

所以我想让它变得通用,说它是一个全局方法或者从不同的 Viewcontroller 调用同一个类的东西

最佳答案

您可以创建一个通用方法来呈现警报 Controller

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)

}
}

从你的 View Controller 调用这个函数

AlertHelper.showAlertWithTitle(self, title: message, dismissButtonTitle: "OK") { () -> Void in

}

关于ios - 如何在 swift iOS 中创建一个通用的弹出 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43209117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com