编辑:接受的答案是满足我的需求,但我仍然对不同的方法持开放态度。
我想出了这个问题。我有一个控制器,我希望它只是表现。如果某个控制器试图推送它或尝试用其他segue显示它,应用程序不应该显示它。让我用一个例子来澄清:
class OnlyPresentableController : UIViewController{
///imagine a variable like this exists.
override var isOnlyPresentable : Bool{
return true
}
//........
}
class SomeController : UIViewController{
//.....
@IBAction func aButtonClick(_ sender: UIButton) {
let controller = OnlyPresentableController(nibName: "OnlyPresentableController", bundle: Bundle.main)
//this will work
present(controller, animated: true, completion: nil)
}
@IBAction func anOtherButtonClick(_ sender: UIButton) {
let controller = OnlyPresentableController(nibName: "OnlyPresentableController", bundle: Bundle.main)
//this will not work because controller is an only presentable one.
navigationController?.pushViewController(controller, animated: true)
}
}
所以你怎么看?这可以实现吗?
所以我对防止推动有了一个想法。你可以使用Swizzling来调配pushViewController
功能。所以在swizzles函数中你检查视图控制器是否是OnlyPresentableController
的实例然后你什么都不做,如果没有你可以继续推动。
注意:我假设你知道Method Swizzling