有没有办法让控制器只在iOS中出现(也许在macOS中)?

问题描述 投票:0回答:1

编辑:接受的答案是满足我的需求,但我仍然对不同的方法持开放态度。

我想出了这个问题。我有一个控制器,我希望它只是表现。如果某个控制器试图推送它或尝试用其他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)
    }

}

所以你怎么看?这可以实现吗?

ios uiviewcontroller segue pushviewcontroller presentviewcontroller
1个回答
1
投票

所以我对防止推动有了一个想法。你可以使用Swizzling来调配pushViewController功能。所以在swizzles函数中你检查视图控制器是否是OnlyPresentableController的实例然后你什么都不做,如果没有你可以继续推动。

注意:我假设你知道Method Swizzling

© www.soinside.com 2019 - 2024. All rights reserved.