【问题标题】:PersonViewController in TabBarControllerTabBarController 中的 PersonViewController
【发布时间】:2015-08-14 02:00:32
【问题描述】:

我有一个带有三个标签的UITabBarController。当按下某个按钮时,我希望用户立即看到一个人视图控制器(ABPersonViewController 类的一个实例)。

我不想只使用带有人员视图控制器的presentViewController() 方法作为参数,因为当用户可以看到它所呈现的底层视图控制器时,这会导致延迟。

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

感谢日航的回答:

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
}

【问题讨论】:

  • 请不要在您的问题中复制/包含答案。如果答案对您有所帮助,您可以投票和/或接受它。但答案不属于问题。

标签: ios swift uitabbarcontroller abaddressbook


【解决方案1】:

回答

本质思想是继承UITabBarController,在标签栏上放一个UIButton,覆盖原来的标签区域,给那个按钮添加一个动作,触发相关的函数调用。

这样点击事件实际上永远不会被传递给 tabBarController,而只会传递给按钮。这样用户就永远不会“看到底层视图控制器”。

示例代码

将按钮添加到 UITabBarController 中心的 Swift 1.2 示例代码

func AddSampleButton(){
    let SampleButton = UIButton()
    SampleButton.setTranslatesAutoresizingMaskIntoConstraints(false)
    SampleButton.setBackgroundImage(addImage, forState: UIControlState.Normal)

    SampleButton.addConstraint(NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: self.view.frame.size.width/5))

    SampleButton.addConstraint(NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50))

    self.view.addSubview(addButton)

    self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: addButton, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))

    self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: addButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    SampleButton.enabled = true
    SampleButton.addTarget(self, action: "SampleAction", forControlEvents: UIControlEvents.TouchUpInside)
}

func SampleAction() {
    let abpvc = ABPersonViewController()
    abpvc.personViewDelegate = self
    self.navigationController?.pushViewController(abpvc, animated: true)
}

参考

http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/

通知

如果您没有看到该按钮,请注意 UIViewContoller 的生命周期,因为这可能是您的错误的根源。

【讨论】:

    【解决方案2】:

    使用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
    
    }
    

    要选择标签栏项目(如果您使用的是图像),请更改UITabBarItemimageselectedImage。因为您在选择选项卡时返回 false,所以您可能需要更改 image 属性而不是 selectedImage

    self.tabBar.items?.first?.image = UIImage(...)
    

    【讨论】:

    • 这个方法第一行的断点永远不会到达,这表明当一个标签栏项目被按下并出现一个新的vc时它实际上并没有被调用。
    • 您是否将标签栏委托属性设置为您调用该方法的类?
    • 我们是在谈论UITabBarDelegate 还是UITabBarController 代表?你说的是UITabBarDelegate,但是从文档上看,只有UITabBarControllerDelegate有那个方法?
    • 当然可以,只需解开导航控制器的第一个视图控制器。那应该是你想要的视图控制器。
    • 有什么办法可以修改这个,以便我们拦截的标签栏项目在按下时突出显示?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    相关资源
    最近更新 更多