【问题标题】:Swift: How to access in AppDelegate variable from the View controller?Swift:如何从 View 控制器访问 AppDelegate 变量?
【发布时间】:2015-09-08 23:32:26
【问题描述】:

我想在文本或 csv(首选)文件中写入来自视图控制器的变量。

确实,我正在做一个绘图应用程序,我想在文件中写入手指的当前位置。

class ViewController: UIViewController {var lastPoint = CGPoint.zeroPoint ... }

我想从 AppDelegate 访问 lastPoint.x 和 lastPoint.y。我怎么能这样做?谢谢。

【问题讨论】:

  • 您的 ViewController 是在哪里以及如何创建的?

标签: swift viewcontroller appdelegate


【解决方案1】:

你的问题充满了困惑,但如果这就是你要找的:

您可以通过获取对它的引用来访问 appDelegate:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

之后,如果您在 appDelegate 中存储了一个名为 lastPoint 的属性,您可以像这样非常简单地访问它的组件:

let x = appDelegate.lastPoint.x
let y = appDelegate.lastPoint.y

如果您需要从 AppDelegate 访问您的 viewController 属性,那么我建议您在 appdelegate 中引用您的视图控制器:

var myViewController: ViewController!

然后,当您的视图控制器被创建时,您可以在 appdelegate 属性中存储对它的引用:

如果您在 appDelegate 之外创建视图控制器:

Swift 1-2 语法

var theViewController = ViewController()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.myViewController = theViewController

Swift 3-4 语法

var theViewController = ViewController()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.myViewController = theViewController

如果您在 appDelegate 中创建视图控制器:

self.myViewController = ViewController()

之后,您可以从 appdelegate 的 viewcontroller 访问您的数据,只需像这样访问它的属性:

let x = self.myViewController.lastPoint.x
let y = self.myViewController.lastPoint.y

【讨论】:

  • 其实我需要的正好相反。从 appDelegate 我需要访问变量 lastPoint.x 和 lastPoint.y(在 ViewController 中找到)
  • 它不起作用。这一行有一个错误:appDelegate.myViewController = theViewController "expected declaration" 还有:let x = self.myViewController.lastPoint.x let y = self.myViewController.lastPoint.y
  • 你能告诉我们你把这些行放在哪里了吗?
  • inside: class AppDelegate: UIResponder, UIApplicationDelegate {...} (而不是3个点)因为我想在应用程序函数中使用X和Y。
  • 所有这些都需要在函数体中声明。例如,您可以将其放在 applicationDidFinishLaunching 中,也可以放在创建 ViewController 时可以调用的自定义函数中
【解决方案2】:

Swift 3 更新

    var theViewController = ViewController()
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.myViewController = theViewController

【讨论】:

    【解决方案3】:

    你可以创建一个 BaseViewController 并写这个

    class BaseViewController {
      lazy var appDelegate : AppDelegate {
         return UIApplication.shared.delegate as? AppDelegate
      }
    }
    

    并使用 BaseViewController 继承其他视图控制器并通过

    访问它
    class ViewController : BaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print(self.appDelegate?.lastPoint.x)
        print(self.appDelegate?.lastPoint.x)
    }}
    

    【讨论】:

      【解决方案4】:

      我确实设法通过在 AppDelegate.swift 中创建 ViewController 的实例来从 App 委托访问我的 ViewController,例如:

      var mainViewController = ViewController()
      
      func applicationWillEnterForeground(_ application: UIApplication) {
          mainViewController.myVariable = "Hello".
      }
      

      不过,我不明白 AppDelegate 如何“知道” mainViewController 应该指向那个特定的 ViewController。由于我的应用程序只有一个视图和一个 ViewController,这很好,但是我有多个 ViewController 与不同的 UIView 关联呢?感谢这里是否有人可以对此有所了解。 最好的祝福, 安德烈

      【讨论】:

      • 刚刚发现 AppDelegate 没有引用我实际的 ViewController.swift。它似乎在我的 AppDelegate 中创建了一个新的 ViewController 实例,所以我仍然不知道如何访问我现有的 VC 实例。感谢您对此主题的任何澄清。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      相关资源
      最近更新 更多