一般删除导航堆栈中的自定义 UIViewController class
Remove a custom UIViewController class inside Navigation stack generically
我想以编程方式删除 UINavigationController
堆栈中某些特定类型的控制器。
工作非泛型函数:
if let navigationController = navigationController {
var controllers = [AnyObject]()
for item in navigationController.viewControllers {
if !(item is CustomViewController) {
controllers.append(item)
}
}
navigationController.viewControllers = controllers
}
但是,我正在努力使这个变得通用。
func removeController<T>(controller: T.Type, navigationController: UINavigationController?) {
if let navigationController = navigationController {
var controllerArray = [AnyObject]()
for item in navigationController.viewControllers {
if !(item is T.Type) {
controllerArray.append(item)
}
}
navigationController.viewControllers = controllerArray
}
}
removeController(CustomViewController.self, navigationController)
多次尝试,还是不行。
谁能帮帮我,谢谢
试试下面的代码:
func removeController<T>(type: T.Type, navigationController: UINavigationController?) {
if let navigationController = navigationController {
var controllerArray = [UIViewController]()
for item in navigationController.viewControllers as [UIViewController] {
if !(item is T) {
controllerArray.append(item)
}
}
navigationController.viewControllers = controllerArray
}
}
removeController(CustomViewController.self, navController)
我想以编程方式删除 UINavigationController
堆栈中某些特定类型的控制器。
工作非泛型函数:
if let navigationController = navigationController {
var controllers = [AnyObject]()
for item in navigationController.viewControllers {
if !(item is CustomViewController) {
controllers.append(item)
}
}
navigationController.viewControllers = controllers
}
但是,我正在努力使这个变得通用。
func removeController<T>(controller: T.Type, navigationController: UINavigationController?) {
if let navigationController = navigationController {
var controllerArray = [AnyObject]()
for item in navigationController.viewControllers {
if !(item is T.Type) {
controllerArray.append(item)
}
}
navigationController.viewControllers = controllerArray
}
}
removeController(CustomViewController.self, navigationController)
多次尝试,还是不行。
谁能帮帮我,谢谢
试试下面的代码:
func removeController<T>(type: T.Type, navigationController: UINavigationController?) {
if let navigationController = navigationController {
var controllerArray = [UIViewController]()
for item in navigationController.viewControllers as [UIViewController] {
if !(item is T) {
controllerArray.append(item)
}
}
navigationController.viewControllers = controllerArray
}
}
removeController(CustomViewController.self, navController)