[英]Swift / iOS: How to pass data from AppDelegate to ViewController label?
I've got a (very) basic Universal Links test program that correctly launches from a link to the associated domain.我有一个(非常)基本的通用链接测试程序,它可以从链接到关联域正确启动。 It prints the received URL to the debug console.它将接收到的 URL 打印到调试控制台。
in AppDelegate.swift:在 AppDelegate.swift 中:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
// First attempt at handling a universal link
print("Continue User Activity called: ")
if userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let url = userActivity.webpageURL {
//handle URL
let u = url.absoluteString
print(u)
}
return true
}
ViewController.swift:视图控制器.swift:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var result: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print("Ready ..")
result.text = "view did load"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I just want to update the UILabel with the received URL, to show it's working.我只想用收到的 URL 更新 UILabel,以显示它正在工作。
I've tried putting the NSUserActivity handler into ViewController, that compiles but doesn't work.我试过将 NSUserActivity 处理程序放入 ViewController,它编译但不起作用。
I've tried creating a method in ViewController that I can call from AppDelegate, but don't know how to address it (I'm a Swift beginner).我尝试在 ViewController 中创建一个可以从 AppDelegate 调用的方法,但不知道如何解决它(我是 Swift 初学者)。
How can I get the URL to update the UILabel text?如何获取更新 UILabel 文本的 URL?
You can do this if it's the root如果它是根,你可以这样做
if let vc = self.window?.rootViewController as? ViewController {
vc.result.text = u
}
For a complete solution you should get the top most vc with Get top most UIViewController then cast it like above对于完整的解决方案,您应该使用Get top most UIViewController 获得最顶级的 vc 然后像上面一样投射它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.