TabBarController中的PersonViewController

581*_*813 11 uitabbarcontroller abaddressbook ios swift

我有UITabBarController三个选项卡.当按下某一个时,我希望用户立即看到一个人视图控制器(ABPersonViewController该类的一个实例).

我不想仅将该presentViewController()方法与人员视图控制器一起用作参数,因为当用户可以看到它所呈现的基础视图控制器时,这会导致延迟.

我也无法使视图控制器继承ABPersonViewController,因为它是由Apple设置的,因此它不能被子类化.有没有办法可以做到这一点?

感谢JAL的回答:

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

    let navViewController = viewController as! UINavigationController

    // First, check to see if the view controller is the one you want to override
    if let myViewController = navViewController.viewControllers[0] as? ThirdViewController {

        let abpvc = ABPersonViewController()
        abpvc.personViewDelegate = self
        self.navigationController?.pushViewController(abpvc, animated: true)
        return false

    }

    return true
}
Run Code Online (Sandbox Code Playgroud)

JAL*_*JAL -1

使用UITabBarDelegate方法shouldSelectViewController

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

    // First, check to see if the view controller is the one you want to override 
    if let myViewController = viewController as? ViewControllerToOverride {
        // do something here, present a new view controller, etc.
        return false // then return false to prevent the tab from switching

}
Run Code Online (Sandbox Code Playgroud)

要选择选项卡栏项目(如果您使用的是图像),请更改UITabBarItem或。因为选择选项卡时返回 false,所以您可能需要更改属性而不是.imageselectedImageimageselectedImage

self.tabBar.items?.first?.image = UIImage(...)
Run Code Online (Sandbox Code Playgroud)