Ios 当前视图显示黑屏,为什么?
我正在制作一个应用程序,如果某个特定日期已经过去,则在启动应用程序后会显示一个视图控制器。问题是:它只是返回一个黑屏。 我已经测试了它,将显示的视图控制器设置为初始视图控制器,它工作得非常好;没有黑屏。因此,我的错误必须在代码中 我在Ios 当前视图显示黑屏,为什么?,ios,swift,viewcontroller,Ios,Swift,Viewcontroller,我正在制作一个应用程序,如果某个特定日期已经过去,则在启动应用程序后会显示一个视图控制器。问题是:它只是返回一个黑屏。 我已经测试了它,将显示的视图控制器设置为初始视图控制器,它工作得非常好;没有黑屏。因此,我的错误必须在代码中 我在viewdide()中调用此函数。: 这不是实际的代码,它更像是一个简化版本。在我的例子中,日期是数组中的自定义对象。对象将其属性保存为UserDefaults,以便可以在显示的视图控制器中显示它们。MypresentView(fromDate:date)功能如下:
viewdide()中调用此函数。
:
这不是实际的代码,它更像是一个简化版本。在我的例子中,日期是数组中的自定义对象。对象将其属性保存为UserDefaults,以便可以在显示的视图控制器中显示它们。MypresentView(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)
}
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)
但是,当实际调用此函数时,它会显示一个黑屏而不显示任何错误。你能告诉我为什么吗?尝试通过抓取情节提要引用来添加新的ViewController。并使用InstanceViewControllerWithiIdentifier函数。如果您在故事板中创建了新的ViewController,那么这样的实例化将无法从中获得ui界面 您可以尝试在情节提要上为其指定情节提要ID,然后尝试:
func presentView(fromDate: date) {
let newViewController = self.storyboard.instantiateViewController(withIdentifier: "NewControllerIdentifier")
// your code
self.present(vc, animated: true, completion: nil)
}
如果控制器与演示控制器位于同一故事板中。或者你可以创建一个segue到它,调用segue并在“prepareforsgue”中设置新的控制器数据。在我的例子中,我也经常使用各种
视图控制器
,即UIAlertController
,一个自定义nib UIViewController
,一个故事板UIViewController
,和一个硬编码UIViewController
。我不确定这是否是Apple
上的错误,但为了解决这个问题,我在viewdide
上显示了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;
要在视图中显示它,请单击“显示”。然而,我发现我不得不使用延迟值,因为在模拟器中它可以使用0.2,但在设备上可能不会。苹果不能为开发者提供一个可以安全显示新视图的活动,这真是令人恼火
希望这对某人有所帮助。尝试以下方法:
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)
}
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)
我遇到了同样的问题,我试图在按下按钮时显示视图控制器。最初,我尝试以下内容:-
@IBAction func buttonPressed(_ sender: Any) {
let vc = PaperViewController()
self.present(vc, animated: true, completion: nil)
}
为了解决这个错误,我添加了故事板Id,在我的案例中是“纸”,并使用了为我工作的InstanceEviewController
@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)
在我的例子中,对于.xib文件,问题是没有设置.xib文件所有者的目标成员身份 要查看它是否适用于您的情况,请检查文件检查器的“目标成员资格”部分
NewViewController中有什么?它可能只是一个空的黑屏。它只是一个带有一些标签的普通视图控制器。。。不要认为这就是问题所在……这段代码是从启动viewcontroller执行的吗?你有没有试着推迟处决?是的。。。也不起作用:(因为您的NewViewController是一个自定义类。如果它在情节提要中,您可以在属性检查器中更改背景色。或者在自定义类的viewDidLoad中执行view.backgroundColor=UIColor.green。或者从这一行后面的代码“let vc=NewViewController()'add vc.backgroundColor=UIColor.green也不起作用…也许你不能在ViewDidAspect()中调用此函数?你认为如何?这个答案可以改进,只需解释一下为什么值得尝试。