【问题标题】:How to pass data from appdelegate to a viewcontroller?如何将数据从 appdelegate 传递到视图控制器?
【发布时间】:2017-05-03 20:49:04
【问题描述】:

您好,我正在尝试在我的 IOS 应用程序中实现推送通知功能。目前,如果应用程序是通过推送通知打开的,我有代码将打开一个特定的视图控制器。我这样做的方式是将视图控制器推到顶部。

我现在需要做的是将一些数据从 appdelegate 传递给 viewcontroller。我知道将数据从视图控制器传递到视图控制器是使用prepareforsegue。我试过这样做,但它不起作用

我尝试研究如何完成这项任务,但很多关于 stackoverflow 的答案都是过时的 swift 代码,我无法转换为 swift 3。有人可以向我解释我如何将数据从 appdelegate 发送到视图控制器?

这里是显示在 didFinishLaunchingWithOptions 中的 VC 的代码

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let destinationViewController = storyboard.instantiateViewController(withIdentifier: "detailPin2") as! detailPinViewController

let navigationController = self.window?.rootViewController as! UINavigationController

navigationController.pushViewController(destinationViewController, animated: false)

【问题讨论】:

  • 显示您用于显示来自 AppDelegate 的 VC 的代码。
  • 已编辑帖子。代码现在应该已经启动了
  • 我错过了什么吗?您在 appdelegate 的一个函数中,并且有一个指向目标 VC 的指针。你不能用你打算传递的数据调用你的 VC 的成员函数吗?

标签: ios swift3 appdelegate apn


【解决方案1】:
//Declare you variable  somewhere within the app delegate scope
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var myVariable: String = "Hello Swift"


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }
 // some other app delegate’s methods.... 

}

//In your view controller
class ViewController: UIViewController {

    @IBOutlet weak var myLabel: UILabel!
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let myOtherVariable = appDelegate.myVariable

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        myLabel.text = myOtherVariable
        var anotherVariable: String = appDelegate.myVariable // etc...

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

【讨论】:

    【解决方案2】:

    好的 - 你已经完成了大部分工作......

    当使用 segues 时,iOS 会创建你的视图控制器并让你在准备中访问它,你可能已经做了这样的事情:

    if let vc = segue.destination as? detailPinViewController {
         vc.myVariable = "this is the data to pass"  
    }
    

    didFinishLaunchingWithOptions 中,您正在进行创建部分和“演示”部分......所以您只需添加“传递数据”部分:

    let destinationViewController = storyboard.instantiateViewController(withIdentifier: "detailPin2") as! detailPinViewController
    
    destinationViewController.myVariable = "this is the data to pass"
    
    let navigationController = self.window?.rootViewController as! UINavigationController
    
    navigationController.pushViewController(destinationViewController, animated: false)
    

    应该这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多