Swift 3 - 将 rootviewcontroller 委托设置为导航控制器
Swift 3 - setting rootviewcontroller delegate to navigation controller
在我的 Swift 3/Obj-C 应用程序中,我试图将我的 rootviewcontroller 的委托设置为其导航控制器,但出现以下错误:
Cannot assign value of type 'NavController.Type' to 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];
}
关于如何解决该错误的任何想法?
您收到该错误是因为行
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
}
在我的 Swift 3/Obj-C 应用程序中,我试图将我的 rootviewcontroller 的委托设置为其导航控制器,但出现以下错误:
Cannot assign value of type 'NavController.Type' to 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];
}
关于如何解决该错误的任何想法?
您收到该错误是因为行
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
}