我正在我的应用程序中使用UITabBarController。我想从其中一个标签的子视图访问在标签栏中定义的变量:
let tabBar = tabBarController as! baseTabBarController
我已经在另一个标签页上完成了它,并且工作正常。但是这次,我得到一个错误:
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/ms/Desktop/invoice/invoice/PreviewViewController.swift, line 36
2020-05-30 18:46:26.511904+0200 Invo[9455:2542848] Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/ms/Desktop/invoice/invoice/PreviewViewController.swift, line 36
请帮助!谢谢。
这里是视图控制器类:
import UIKit
import WebKit
class PreviewViewController: UIViewController {
@IBOutlet var webPreview: UIWebView!
var invoiceComposer: InvoiceComposer!
var HTMLContent: String!
var invoiceNumber = 0
override func viewDidLoad() {
super.viewDidLoad()
print(invoiceNumber)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
createInvoiceAsHTML()
}
func createInvoiceAsHTML() {
invoiceComposer = InvoiceComposer()
if let tabBar = tabBarController as? baseTabBarController {
if let invoiceHTML = invoiceComposer.renderInvoice(invoiceNumber: String(invoiceNumber), invoiceDate: "", recipientInfo: tabBar.customerData[invoiceNumber][0].input, items: tabBar.itemsData[invoiceNumber], totalAmount: tabBar.totalData[invoiceNumber].total) {
webPreview.loadHTMLString(invoiceHTML, baseURL: NSURL(string: invoiceComposer.pathToInvoiceHTMLTemplate!)! as URL)
HTMLContent = invoiceHTML
}
print("works")
}
else {
print("tabBarController is not of type baseTabBarController or either nil ")
}
}
}
这里是情节提要的视图:
这里是标签栏控制器的实现:
导入UIKit
class baseTabBarController: UITabBarController {
var customerData: [[customerInformation]] = []
var itemsData: [[Item]] = []
var totalData: [TotalInformation] = []
override func viewDidLoad() {
super.viewDidLoad()
}
tabBarController的类型不是baseTabBarController或nil您可以检查
if let tabBar = tabBarController as? baseTabBarController {
print("tabBarController exists and of type baseTabBarController")
} else {
print("tabBarController is not of type baseTabBarController or either nil ")
}