簡體   English   中英

當前視圖顯示黑屏,為什么?

[英]Present view shows black screen, why?

我正在制作一個應用程序,如果特定日期已過,則在啟動應用程序后會顯示一個視圖控制器。 問題是:它只是返回一個黑屏。 我已經測試過它將呈現的視圖控制器設置為初始視圖控制器,並且它運行良好; 沒有黑屏。 因此,我的錯誤必須在代碼中。

我在viewDidAppear()調用這個函數:

if date.hasPassed {
        presentView(fromDate: date)
    }

這不是實際的代碼,它更像是一個簡化版本。 日期在我的例子中是一個來自數組的自定義對象。 該對象將其屬性保存到 UserDefaults,以便它們可以在出現的視圖控制器中顯示。 我的presentView(fromDate: date)函數如下:

func presentView(fromDate: date) {
    let vc = NewViewController()

    let title = date.title ?? "My date"
    let description = date.description ?? "My description"

    UserDefaults.standard.set(title, forKey: "title")
    UserDefaults.standard.set(description, forKey: "description")
    UserDefaults.standard.synchronize()

    self.present(vc, animated: true, completion: nil)
}

但是當實際調用這個函數時,它會顯示一個黑屏而不顯示任何錯誤。 你能告訴我為什么嗎?

如果您在故事板中創建了 NewViewController,那么像這樣實例化將不會從那里獲得 ui 界面。

您可以嘗試在情節提要上為其分配 StoryBoardId 並嘗試:

func presentView(fromDate: date) {
   let newViewController = self.storyboard.instantiateViewController(withIdentifier: "NewControllerIdentifier")

   // your code

  self.present(vc, animated: true, completion: nil)
}

如果控制器與您的演示控制器在同一個故事板中。 或者您可以為它創建一個 segue,調用 segue 並在“prepareForSegue”中設置新的控制器數據。

嘗試通過獲取故事板參考來添加 newviewcontroller。 並使用instantiateviewcontrollerwithidentifier 函數。

我在嘗試在按下按鈕時顯示視圖控制器時遇到了同樣的問題。 最初我嘗試使用以下方法:-

    @IBAction func buttonPressed(_ sender: Any) {
        let vc = PaperViewController()
        self.present(vc, animated: true, completion: nil)
    }

為了解決這個錯誤,我添加了故事板 ID,在我的案例中它是“紙”,並使用了對我有用的 instantiateViewController。

   @IBAction func buttonPressed(_ sender: Any) {
        if let vc = self.storyboard?.instantiateViewController(withIdentifier: 
     "paper") {
        self.present(vc, animated: true, completion: nil)
    }
 }

如果是XIB情況,請仔細檢查VCXIB是否具有相同的名稱:

不正確:

DebugLogsVC.swift
DebugLogs.xib

正確:

DebugLogsVC.swift
DebugLogsVC.xib

展示:

let vc = DebugLogsVC()
self.present(vc, animated: true, completion: nil)

就我而言,使用 .xib 文件時,問題在於未設置 .xib 文件所有者的目標成員資格。

要查看它是否適用於您的情況,請檢查文件檢查器的“目標成員資格”部分。

在此處輸入圖片說明

這對我來說也經常發生,在我使用各種viewControllers情況下,即UIAlertControllerCustom nib UIViewControllerStoryboard UIViewControllerHard Coded UIViewController 我不確定這是否是Apple上的錯誤,但為了解決這個問題,我在一段時間延遲后在viewDidAppear上顯示了viewController

這是我的代碼。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // 1 second delay so the views will render first before presenting the alert
    Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.presentTheAlert), userInfo: nil, repeats: false)
}

func presentTheAlert() {
    let alertController = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
    self.present(alertController, animated: true, completion: nil)
}

所以本質上它的作用是,它在呈現另一個viewController之前等待視圖完全呈現。

我喜歡把視圖控制器的表示放在一個像你一樣的方法中並使用

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;

在 viewDidAppear 中顯示它。 但是,我發現我不得不使用延遲值,因為在模擬器中它適用於 0.2,但在設備上可能不會。 Apple 無法為開發人員提供一個安全的事件來顯示新視圖,這真的很煩人。

希望這可以幫助某人。

嘗試以下操作:

let bundle = Bundle.main
let storyboard = UIStoryboard(name: "Main", bundle: bundle)
var newViewController: UIViewController!
newViewController = storyboard.instantiateViewController(withIdentifier: "NewControllerIdentifier")
present(newViewController, animated: true, completion: nil)

暫無
暫無

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

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