Mic*_*lum 706
另一个解决方案是正确的,它将为您提供对应用程序委托的引用,但这不允许您访问由UIApplication的子类添加的任何方法或变量,如托管对象上下文.要解决这个问题,只需简单地转发到"AppDelegate"或者你的UIApplication子类恰好被调用.像这样:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let aVariable = appDelegate.someVariable
Run Code Online (Sandbox Code Playgroud)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let aVariable = appDelegate.someVariable
Run Code Online (Sandbox Code Playgroud)
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let aVariable = appDelegate.someVariable
Run Code Online (Sandbox Code Playgroud)
Dog*_*fee 39
Swift 4.2
在Swift中,易于访问VC
extension UIViewController {
var appDelegate: AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
}
Run Code Online (Sandbox Code Playgroud)
AiO*_*OsN 23
在代码末尾添加AppDelegate类
func appDelegate() -> AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
Run Code Online (Sandbox Code Playgroud)
在你的班级中使用AppDelegate参考?
调用AppDelegate方法
的appDelegate().setRoot()
Con*_*nor 17
它与Objective-C几乎相同
let del = UIApplication.sharedApplication().delegate
Run Code Online (Sandbox Code Playgroud)
jim*_*b99 17
这可以用于OS X.
let appDelegate = NSApplication.sharedApplication().delegate as AppDelegate
var managedObjectContext = appDelegate.managedObjectContext?
Run Code Online (Sandbox Code Playgroud)
Pau*_*anu 12
这是Swift 2.0版本:
let delegate = UIApplication.sharedApplication().delegate as? AppDelegate
Run Code Online (Sandbox Code Playgroud)
并访问托管对象上下文:
if let delegate = UIApplication.sharedApplication().delegate as? AppDelegate {
let moc = delegate.managedObjectContext
// your code here
}
Run Code Online (Sandbox Code Playgroud)
或者,使用警卫:
guard let delegate = UIApplication.sharedApplication().delegate as? AppDelegate else {
return
}
let moc = delegate.managedObjectContext
// your code here
Run Code Online (Sandbox Code Playgroud)
在AppDelegate Class中为ex创建一个方法
func sharedInstance() -> AppDelegate{
return UIApplication.sharedApplication().delegate as! AppDelegate
}
Run Code Online (Sandbox Code Playgroud)
并称之为其他地方的ex
let appDelegate : AppDelegate = AppDelegate().sharedInstance()
Run Code Online (Sandbox Code Playgroud)
func sharedInstance() -> AppDelegate{
return UIApplication.shared.delegate as! AppDelegate
}
Run Code Online (Sandbox Code Playgroud)
小智 6
简单尝试一下:
斯威夫特4
// Call the method
(UIApplication.shared.delegate as? AppDelegate)?.whateverWillOccur()
Run Code Online (Sandbox Code Playgroud)
在您的AppDelegate中:
// MARK: - Whatever
func whateverWillOccur() {
// Your code here.
}
Run Code Online (Sandbox Code Playgroud)
确保你 import UIKit
let appDelegate = UIApplication.sharedApplication().delegate! as! AppDelegate
这是用于UIApplicationDelegate避免对AppDelegate类名进行硬编码的扩展:
extension UIApplicationDelegate {
static var shared: Self {
return UIApplication.shared.delegate! as! Self
}
}
// use like this:
let appDelegate = MyAppDelegate.shared // will be of type MyAppDelegate
Run Code Online (Sandbox Code Playgroud)
小智 5
这很简单
应用程序委托实例
let app = UIApplication.shared.delegate as! AppDelegate
Run Code Online (Sandbox Code Playgroud)
您可以使用一行语法调用方法
app.callingMethod()
Run Code Online (Sandbox Code Playgroud)
您可以使用此代码访问变量
app.yourVariable = "Assigning a value"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
194658 次 |
| 最近记录: |