簡體   English   中英

Swift3.0 我想獲取 AppDelegate 的對象

[英]Swift3.0 I want to get object of AppDelegate

我想獲得 AppDelegate 的對象。 該程序可以構建,但會因 lldb 錯誤而停止運行。

也許問題是 Swift2.0 和 3.0 的問題。 我的教科書是針對 swift2.0 的,但我使用的是 xcode8.0 和 Swift3.0。

錯誤在這里。

讓 ap = UIApplication.shared.delegate as! 應用程序委托

我使用此頁面進行修復。

如何在 Swift 中獲取對應用程序委托的引用?

import UIKit

class FirstViewController: UIViewController {

@IBOutlet weak var dataTextField: UITextField!


let ap = UIApplication.shared.delegate as! AppDelegate


override func viewWillAppear(_ animated: Bool) {
    dataTextField.text = String(ap.cmValue)
}


@IBAction func tapInpu() {

    dataTextField.resignFirstResponder()
    if let text = dataTextField.text{

        if let cmValue = Double(text){
        ap.cmValue = cmValue
        }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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


}

如果刪除ap初始化和ap.cmValue = cmValue ,它是否有效?

如果它確實有效,請檢查您的故事板中的插座引用,您可能有舊的不存在的引用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM