I am not using storyboard.
I made customTabBarView. (I added 2 button on View).
I connected the buttons with the same function and gave the buttons a tag
value.
How can I open the ViewController when I press the first button or press any button?
When I tap first Button I got this error.
Error:
*** 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
SceneDelegate:
guard let windowScene = (scene as? UIWindowScene) else { return }
let countryRouter = TabBarViewController()
let window = UIWindow(windowScene: windowScene)
window.rootViewController = countryRouter
self.window = window
window.makeKeyAndVisible()
ViewControllerCountryList:
class ViewControllerCountryList: UIViewController, CountryListModule.View {
.
.
.
}
TabBarButton:
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
}
}
}
2
Answers
The error is saying that you’ve loaded a
NSObject
and tried to use it as if it had acountryListTableView
member. This suggests that the first item in your xib is not aViewControllerCountryList
.If looking at the xib doesn’t lead to an obvious solution, I suggest debugging by examining what is actually returned from loading the xib (rather than immediately trying to cast it).
Using
instantiate(withOwner:options:)
is not really suitable for your task.A better method is to use
init(nibName:bundle:)
.This extension wraps it into an easy one-line call:
With that, you can now do:
For your "custom tab" layout, take a look at this: