在我的Swift 3 / Obj-C应用程序中,我试图将rootviewcontroller的委托设置为导航控制器,但是收到以下错误:
无法指定类型' NavController.Type'的值输入 ' RootViewControllerDelegate'
NavController.swift
@available(iOS 10.0, *)
@objc class NavController: UINavigationController, RootViewControllerDelegate {
static func instantiate() -> NavController? {
let storyboard = UIStoryboard.init(name: "MyStoryboard", bundle: nil)
let navVC = storyboard.instantiateViewController(withIdentifier: "NavController") as? NavController
let rootVC = navVC?.viewControllers.first as! RootViewController
rootVC.delegate = self // <<<< ERROR OCCURS HERE
return navVC
}
func pressedNextButton()
{
NSLog("Next button works!")
}
}
RootViewController.swift
@objc protocol RootViewControllerDelegate {
func pressedNextButton()
}
@available(iOS 10.0, *)
@objc class RootViewController: UIViewController, NSFetchedResultsControllerDelegate, UITableViewDataSource, UITableViewDelegate {
var delegate: RootViewControllerDelegate?
@IBAction func nextButton(_ sender: UIBarButtonItem) {
self.delegate?.pressedNextButton()
}
}
MainProgram.m
@interface MYQAddDeviceViewController ()
@property (nonatomic, strong) NavController * navVC;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
self.navVC = [NavController instantiate];
}
关于如何解决该错误的任何想法?
答案 0 :(得分:3)
您收到该错误是因为该行
rootVC.delegate = self
位于static func
内,在此上下文中,self
指的是NavController
元类的实例,即NavController.Type
而不是NavController
。
你想:
if let navVC = storyboard.instantiateViewController(withIdentifier: "NavController") as? NavController,
let rootVC = navVC.viewControllers.first as? RootViewController {
rootVC.delegate = navVC
}