[英]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
情況,請仔細檢查VC
和XIB
是否具有相同的名稱:
不正確:
DebugLogsVC.swift
DebugLogs.xib
正確:
DebugLogsVC.swift
DebugLogsVC.xib
展示:
let vc = DebugLogsVC()
self.present(vc, animated: true, completion: nil)
這對我來說也經常發生,在我使用各種viewControllers
情況下,即UIAlertController
, Custom nib UIViewController
, Storyboard UIViewController
和Hard 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.