我正在尝试创建一个AppCoordinator
来在我的应用中保存不同的ViewControllers
:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let appCoordinator = AppCoordinator()
window?.rootViewController = appCoordinator.initialTabBarController()
window?.makeKeyAndVisible()
return true
}
// ...
import SnapKit
class AppCoordinator {
private let initialTabController = TabBarController()
private let cars = CarProvider.sharedInstance.getCars()
init() {
var viewControllers = [ViewController]()
viewControllers.append(carListViewController())
initialTabController.setViewControllers(viewControllers, animated: true)
}
private func carListViewController() -> CarListViewController {
let controller = CarListViewController(cars: cars)
print(self)
controller.delegate = self
return controller
}
}
// MARK: - CarListViewControlerDelegate
extension AppCoordinator: CarListViewControlerDelegate {
func didPullToRefresh() {
print("Did pull")
}
}
我的CarListViewController
如下:
import UIKit
protocol CarListViewControlerDelegate: class {
func didPullToRefresh()
}
class CarListViewController: ViewController {
weak var delegate: CarListViewControlerDelegate?
// MARK: Interface Properties
private let tableView = TableView()
private let refreshControl = UIRefreshControl()
private var cars: [Car]?
// MARK: Initializers
init(cars: [Car]) {
super.init(nibName: nil, bundle: nil)
self.cars = cars
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// MARK: - Actions
extension CarListViewController {
@objc private func refresh() {
delegate?.didPullToRefresh()
refreshControl.endRefreshing()
}
}
当我在UITableView
中进行刷新时,将调用我的refresh方法,但是协议方法didPullToRefresh
则没有。当我检查委托时,其值为nil
。
答案 0 :(得分:1)
您需要一个强大的参考,因此请在AppDelegate
内将其设为实例
let appCoordinator = AppCoordinator()
OR
var appCoordinator:AppCoordinator!
self.appCoordinator = AppCoordinator()
答案 1 :(得分:0)
正如Sh_Khan所说,您需要提供强有力的参考。您应该更改为创建的CarListViewController
设置委托的方式。
我如下更改了您的代码。让我知道问题是否解决。
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let appCoordinator = AppCoordinator()
window?.rootViewController = appCoordinator.initialTabBarController()
window?.makeKeyAndVisible()
return true
}
// ...
import SnapKit
class AppCoordinator {
private let initialTabController = TabBarController()
private let cars = CarProvider.sharedInstance.getCars()
var controller = CarListViewController!
init() {
var viewControllers = [ViewController]()
let carListVC = CarListViewController(cars: cars)
self.controller = carListVC
self.controller.delegate = self
viewControllers.append(self.controller)
initialTabController.setViewControllers(viewControllers, animated: true)
}
}
// MARK: - CarListViewControlerDelegate
extension AppCoordinator: CarListViewControlerDelegate {
func didPullToRefresh() {
print("Did pull")
}
}