[英]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.