IOS - Swift - 向BarButtonItem的customView添加目标和操作 [英] IOS - Swift - adding target and action to BarButtonItem's customView

查看:87
本文介绍了IOS - Swift - 向BarButtonItem的customView添加目标和操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个自定义视图,将uiimageview作为子视图。然后我在导航栏中将该自定义视图用作rightBarButtonItem。这适用于在适当的位置显示正确的图标,但无论出于何种原因,我在动作中定义的函数永远不会被调用,因此当我点击rightBarButtonItem时不执行segue。奇怪的是,如果我不插入自定义视图,而是注释掉该位,只需为rightItem设置标题,目标和操作,那么该函数将正确执行。以某种方式添加自定义视图混淆目标和动作属性,但我似乎无法弄清楚如何解决它。任何帮助将不胜感激!

I'm creating a custom view, with a uiimageview as a subview. I'm then using that custom view in my navigation bar as the rightBarButtonItem. This works to display the proper icon in the proper location, but for whatever reason the function I define in "action" is never being called, so the segue isn't performed when I tap on the rightBarButtonItem. Oddly enough if I do NOT insert a custom view, but instead comment that bit out and just set a title, target, and action for rightItem then the function is performed properly. somehow adding that custom view messes with the target and action properties, but I can't seem to figure out how to fix it. Any help would be greatly appreciated!

//create custom view for right item and set it
    var imageViewRight:UIImageView = UIImageView()
    imageViewRight.frame = CGRectMake(5, 10, 35, 25)
    let rightImage:UIImage = UIImage(named: "cameraIconInactive")!
    imageViewRight.image = rightImage
    var rightView:UIView = UIView()
    rightView.frame = CGRectMake(0, 0, 45, 45)
    rightView.addSubview(imageViewRight)
    var rightItem:UIBarButtonItem = UIBarButtonItem()
   //this line right below is the problem - if I comment it out and replace with a simple rightItem.title = "test" and leave everything else the same, then the method runs properly
    rightItem.customView = rightView
    rightItem.target = self
    rightItem.action = "pushProfileToCamera"
    self.navigationItem.rightBarButtonItem = rightItem

}

func pushProfileToCamera(){
    println("pushing profile to camera")
    self.performSegueWithIdentifier("pushProfileToCamera", sender: nil)
}

编辑:

实际上坐在这上面在我看到这些答案建议之前24小时并提出了这个解决方案..任何理由我都不应该这样做?它有效..

actually sat on this for 24 hours and came up with this solution before I saw these answer suggestions.. any reason I shouldn't do this? It works..

//create the custom rightBarButtonItem
    //create the imageView with icon
    var imageViewRight:UIImageView = UIImageView()
    imageViewRight.frame = CGRectMake(5, 10, 35, 25)
    var rightImage:UIImage = UIImage(named: "cameraIconInactive")!
    imageViewRight.image = rightImage
    //put the imageView inside a uiview
    var rightView:UIView = UIView()
    rightView.frame = CGRectMake(0, 0, 45, 45)
    rightView.addSubview(imageViewRight)
    //create the tap gesture recognizer for that uiview
    var rightGestureRecognizer:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "pushProfileToCamera")
    rightView.addGestureRecognizer(rightGestureRecognizer)
    //create the uibarbuttonitem, assign our custom view to it, and insert it in the nav bar!
    var rightItem:UIBarButtonItem = UIBarButtonItem()
    rightItem.customView = rightView
    self.navigationItem.rightBarButtonItem = rightItem


推荐答案

您可以尝试代替链接rightItemAction并将UIView添加到customView,将UIButton添加到rightBarButtonItem:

You can try to instead of linking the rightItemAction and adding a UIView to the customView, add a UIButton to the rightBarButtonItem:

    var button: UIButton = UIButton()
    button.setImage(UIImage(named: "cameraIconInactive"), forState: .Normal)
    button.frame = CGRectMake(0, 0, 45, 45)
    button.targetForAction("pushProfileToCamera", withSender: nil)

    var rightItem:UIBarButtonItem = UIBarButtonItem()
    rightItem.customView = button
    self.navigationItem.rightBarButtonItem = rightItem

这篇关于IOS - Swift - 向BarButtonItem的customView添加目标和操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆