gpt4 book ai didi

ios - View 层次结构错误 - 试图在新 Storyboard中打开新 View Controller

转载 作者:行者123 更新时间:2023-11-28 16:17:58 24 4
gpt4 key购买 nike

我遇到 viewheirarchy 错误 - 试图在新 Storyboard中打开新 View Controller 。

    override func viewDidLoad() {  
super.viewDidLoad()

if type == .Products {
self.presentViewController( UIStoryboard(name: "Fold", bundle: nil).instantiateViewControllerWithIdentifier("MainTableViewController") as! UITableViewController, animated: true, comp
}
}

最佳答案

我建议创建一个私有(private)变量

private var storyboard = UIStoryboard(name: "Fold", bundle: NSBundle.mainBundle())

创建函数

func showYourViewController() throws -> UINavigationController {
if let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController {
if let mainTableViewController = navigationController.topViewController as? MainTableViewController {
mainTableViewController.delegate = self
} else {
throw StoryboardError.InvalidCast
}
return navigationController
} else {
throw StoryboardError.InvalidCast
}
}

现在可以使用了

override func viewDidLoad() {  
super.viewDidLoad()

if type == .Products {
do {
let yourViewController = try showYourViewController()
setRootViewController(yourViewController)
} catch StoryboardError.InvalidCast {
print("Your View Controller was not of the expected type")
} catch _ {
print("Some other error occurred...")
}
}
}

这是 StoryboardError 枚举

enum StoryboardError: ErrorType {
case InvalidCast
}

关于ios - View 层次结构错误 - 试图在新 Storyboard中打开新 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38877088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com