swift - 如何通过swift中的函数调用打开弹出窗口(单独的视图控制器)
问题描述
新的 swift 用户在这里。我想在普通视图控制器上打开一个弹出窗口(由单独的视图控制器控制)作为函数调用的一部分(即没有按下或类似的按钮)。我怎么写这个?我还想发送信息来影响弹出窗口中显示的图像。
我以前设法通过按下按钮打开一个模式弹出窗口。我真的不知道这些弹出窗口是否有什么特别之处,但如果有的话,我想让上面的弹出窗口看起来一样。
我希望这足以让某人了解我的需要。
解决方案
您可以像这样在超级视图控制器中添加一个方法:
class MainViewController: UIViewController {
// MARK: - View methods
override func viewDidLoad() {
super.viewDidLoad()
}
/// This Method is used for Alert With Action Method
func showAlertViewWithPopAction(message: String, title: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "OK", style: .default) { [weak self] _ in
///add you logic here
}
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}
并像这样在每个控制器中继承此控制器,并从控制器中调用此方法,如下所示:
class FirstViewController: MainViewController {
// MARK: - View methods
override func viewDidLoad() {
super.viewDidLoad()
///Call alert method according to your use
Self.showAlertViewWithPopAction(message: "This is demo alert", title: "Alert")
}
}
推荐阅读
- regex - sed:删除除模式之外的所有内容
- c# - 安装第三方软件作为必备软件以及我的应用程序安装包
- c# - 为什么我的字符串没有被正确比较
- c# - System.InvalidOperationException:必须设置值。为 SQLite 设置 Null 参数
- .net - 将 Angular 模板 Web 应用程序添加到 Azure Service Fabric 集群?
- python - 在 Matplotlib 中的两点之间绘制一条简单的线
- .net - 在 IIS (Host.SystemWeb) 下运行时如何设置 OWIN 键,如 host.AppMode?
- c++ - 尝试运行 gmp 时找不到 mpir.dll
- jquery - 将帖子数据从一个 Web 应用程序 (www.example1.com) 传输到另一个 Web 应用程序 (www.example2.com)
- concurrency - Google Datastore - 每秒并发读/写是多少