简体   繁体   English

Swift / iOS:如何将数据从 AppDelegate 传递到 ViewController 标签?

[英]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.

相关问题 将数据从AppDelegate实时传递到ViewController(iOS)Swift 2 - Pass data from AppDelegate to ViewController in realTime (iOS) Swift 2 如何将数据从AppDelegate传递到ViewController? - How to pass data from AppDelegate to ViewController? 如何将数据从appdelegate传递到ViewController? - How to pass data from appdelegate to a viewcontroller? 将数据从 AppDelegate 传递到 ViewController - Pass data from AppDelegate to ViewController swift ios - 如何从AppDelegate在ViewController中运行函数 - swift ios - How to run function in ViewController from AppDelegate ios DeviceToken:如何将DeviceToken从AppDelegate传递到ViewController? - ios DeviceToken: How to pass deviceToken from AppDelegate to ViewController? 如何将CoreLocation值从一个VIewController传递到iOS中的AppDelegate? - how to pass CoreLocation values from one VIewController to AppDelegate in iOS? iOS Swift:将数据从ViewController传递到UITableViewCell - iOS Swift: Pass data from ViewController to UITableViewCell 如何将数据从 XIB ViewController 传递到 iOS Swift 中的 TabBarController - How to pass data from XIB ViewController to TabBarController in iOS Swift 如何在iOS Swift中将数据从ViewController传递给UITabBarController? - How to pass data from ViewController to UITabBarController in iOS Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM