我沒有使用故事板。
我做了customTabBarView。(我在視圖上添加了 2 個按鈕)。
我連接了具有相同功能的按鈕并給按鈕一個tag值。
當我按下第一個按鈕或按下任何按鈕時,如何打開 ViewController?
當我點擊第一個按鈕時,我收到了這個錯誤。
錯誤:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x28184c020> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key countryListTableView.'
terminating with uncaught exception of type NSException
場景委托:
guard let windowScene = (scene as? UIWindowScene) else { return }
let countryRouter = TabBarViewController()
let window = UIWindow(windowScene: windowScene)
window.rootViewController = countryRouter
self.window = window
window.makeKeyAndVisible()
視圖控制器國家串列:
class ViewControllerCountryList: UIViewController, CountryListModule.View {
.
.
.
}
選項卡按鈕:
class TabBarViewController: UIViewController {
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var tabBarView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
designableTabBarView()
}
private func designableTabBarView() {
tabBarView.layer.cornerRadius = tabBarView.frame.size.height / 3
tabBarView.clipsToBounds = true
}
@IBAction func onClickTabBarButton(_ sender: UIButton) {
switch sender.tag {
case 1:
let nib = UINib(nibName: "ViewControllerCountryList", bundle: nil)
guard let countryListVC = nib.instantiate(withOwner: nil, options: nil).first as? ViewControllerCountryList else { return }
self.addChild(countryListVC)
countryList.didMove(toParent: self)
default:
break
}
}
}

uj5u.com熱心網友回復:
錯誤是說你已經加載了 aNSObject并試圖使用它,就好像它有一個countryListTableView成員一樣。這表明您的 xib 中的第一項不是ViewControllerCountryList.
如果查看 xib 無法找到明顯的解決方案,我建議通過檢查加載 xib 實際回傳的內容進行除錯(而不是立即嘗試強制轉換)。
uj5u.com熱心網友回復:
使用instantiate(withOwner:options:)并不真正適合您的任務。
更好的方法是使用init(nibName:bundle:).
這個擴展將它包裝成一個簡單的單行呼叫:
extension UIViewController {
static func loadFromNib() -> Self {
func instantiateFromNib<T: UIViewController>() -> T {
return T.init(nibName: String(describing: T.self), bundle: nil)
}
return instantiateFromNib()
}
}
有了它,您現在可以執行以下操作:
let countryListVC = ViewControllerCountryList.loadFromNib()
對于您的“自定義選項卡”布局,請查看以下內容:
class TabBarViewController: UIViewController {
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var tabBarView: UIView!
// keep references to the loaded view controllers
var countryListVC: ViewControllerCountryList!
var someOtherVC: SomeOtherViewController!
override func viewDidLoad() {
super.viewDidLoad()
designableTabBarView()
}
private func designableTabBarView() {
tabBarView.layer.cornerRadius = tabBarView.frame.size.height / 3
tabBarView.clipsToBounds = true
}
@IBAction func onClickTabBarButton(_ sender: UIButton) {
switch sender.tag {
case 1:
// remove other VC view from content view
if someOtherVC != nil {
someOtherVC.view.removeFromSuperview()
}
// if we haven't loaded ViewControllerCountryList yet
if countryListVC == nil {
countryListVC = ViewControllerCountryList.loadFromNib()
self.addChild(countryListVC)
contentView.addSubview(countryListVC.view)
countryListVC.didMove(toParent: self)
}
// add ViewControllerCountryList view to contentView
contentView.addSubview(countryListVC.view)
countryListVC.view.frame = contentView.bounds
countryListVC.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
case 2:
// remove country list VC view from content view
if countryListVC != nil {
countryListVC.view.removeFromSuperview()
}
// if we haven't loaded SomeOtherViewController yet
if someOtherVC == nil {
someOtherVC = SomeOtherViewController.loadFromNib()
self.addChild(someOtherVC)
contentView.addSubview(someOtherVC.view)
someOtherVC.didMove(toParent: self)
}
// add SomeOtherViewController view to contentView
contentView.addSubview(someOtherVC.view)
someOtherVC.view.frame = contentView.bounds
someOtherVC.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
default:
break
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/407930.html
標籤:
