我有一個包含一些框架的應用程式。AppDelegate 宣告了一些變數,所以我想在框架中使用這些自定義變數。但不幸的是沒有得到它。我根據這個答案使用這個堆疊溢位鏈接我可以使用應用程式委托方法,如 didFinishLaunchingWithOptions 和其他方法,但無法從應用程式委托獲取自定義變數和方法。
public class SampleManager {
public static let shared = SampleManager()
public weak var delegate: UIApplicationDelegate?//UIApplicationDelegate?
func doWhateverYouWant() {
}
}
我宣告了 SampleManager 并像這樣在 didFinishLaunchingWithOptions 中分配它的委托
SampleManager.shared.delegate = self
現在這個委托允許我訪問默認方法和變數,window
但不允許我自定義方法和變數。請指導我如何訪問應用程式委托中宣告的自定義變數和自定義方法。
uj5u.com熱心網友回復:
那是因為UIApplicationDelegate
不包含您的自定義,只需擴展它
protocol Custom: UIApplicationDelegate {
func yourFunc()
}
class AppDelegate: UIResponder, Custom {
func yourFunc() {}
...
}
class SampleManager {
static let shared = SampleManager()
weak var delegate: Custom?
func doWhateverYouWant() {
delegate?.yourFunc()
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/506895.html