拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 当我点击没有导航的按钮时,如何在xib(ViewController)中打开xib(ViewController)

当我点击没有导航的按钮时,如何在xib(ViewController)中打开xib(ViewController)

白鹭 - 2022-03-07 1975 0 0

我没有使用故事板。

我做了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
    }
  }
}

当我点击没有导航的按钮时,如何在 xib(ViewController) 中打开 xib(ViewController)

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
        }
    }

}
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *