gpt4 book ai didi

ios - 如何从 navigationController viewControllers 中删除特殊 Controller ?

转载 作者:行者123 更新时间:2023-11-30 12:49:37 24 4
gpt4 key购买 nike

我想编写一个带有两个paramsclass方法来从导航 Controller 删除viewController,但是我不知道该怎么办。

我的代码如下,我测试过,没有成功:

class func removeVC(_ fromNav:UINavigationController, _ controller:UIViewController) {

let controllers:NSArray = fromNav.viewControllers as NSArray
for item in controllers {

if (item as AnyObject).isMember(of:controller) { // There is not pass by Xcode

// remove item out of fromNav.viewControllers
}
}
}

如何判断controllerclass是否等于swift中的参数controller

最佳答案

尝试这样

方法1)

class func removeVC(fromNav: UINavigationController, controller: UIViewController) {

let controllers = fromNav.viewControllers
for item in controllers {

if item == controller {
fromNav.viewControllers.remove(at: fromNav.viewControllers.index(of: item)!)
}

}
}

用法:UtilSwift.navRemoveVC(self.navigationController!, self)

方法2):

class func navRemoveVC(_ fromNav:UINavigationController, withControllerClass:AnyClass) {

let controllers = fromNav.viewControllers
for item in controllers {
if (item as AnyObject).isKind(of: withControllerClass.self) {
fromNav.viewControllers.remove(at: fromNav.viewControllers.index(of: item)!)
}
}
}

用法:UtilSwift.navRemoveVC(self.navigationController!, withControllerClass:UserRegisterViewController.self)

关于ios - 如何从 navigationController viewControllers 中删除特殊 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41158507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com