-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceA.swift
More file actions
55 lines (45 loc) · 1.53 KB
/
ServiceA.swift
File metadata and controls
55 lines (45 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// ModuleA.swift
// ModuleRouteExample
//
// Created by GIKI on 2025/2/14.
//
import Foundation
import UIKit
class ServiceA: ServiceAInterface {
func showAlertWithTap() {
// 创建 UIAlertController
let alertController = UIAlertController(
title: "点击了我了",
message: nil,
preferredStyle: .alert
)
// 创建确认按钮
let okAction = UIAlertAction(
title: "知道了",
style: .default,
handler: nil
)
// 添加按钮到 alertController
alertController.addAction(okAction)
// 获取当前最顶层的 ViewController 并显示 alert
if let topViewController = UIApplication.shared.keyWindow?.rootViewController?.topMostViewController() {
topViewController.present(alertController, animated: true, completion: nil)
}
}
}
// 辅助扩展,用于获取最顶层的 ViewController
extension UIViewController {
func topMostViewController() -> UIViewController {
if let presented = presentedViewController {
return presented.topMostViewController()
}
if let navigation = self as? UINavigationController {
return navigation.visibleViewController?.topMostViewController() ?? navigation
}
if let tab = self as? UITabBarController {
return tab.selectedViewController?.topMostViewController() ?? tab
}
return self
}
}