【发布时间】:2014-11-06 08:18:57
【问题描述】:
我正在创建一个自定义视图,其中一个 uiimageview 作为子视图。然后,我在导航栏中使用该自定义视图作为 rightBarButtonItem。这可以在正确的位置显示正确的图标,但无论出于何种原因,我在“action”中定义的函数永远不会被调用,所以当我点击 rightBarButtonItem 时不会执行 segue。奇怪的是,如果我不插入自定义视图,而是将其注释掉并为 rightItem 设置标题、目标和操作,那么该功能就会正确执行。以某种方式添加自定义视图与目标和操作属性混淆,但我似乎无法弄清楚如何修复它。任何帮助将不胜感激!
//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 小时并想出了这个解决方案。有什么理由我不应该这样做?它的工作原理..
//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
【问题讨论】:
标签: ios iphone uinavigationbar uinavigationitem