【发布时间】:2018-04-25 02:03:53
【问题描述】:
我正在尝试向导航栏中的自定义视图添加操作。我让它显示得很好,但我不知道如何向它添加点击动作。
我尝试在视图中添加一个按钮并在那里处理它。我尝试让我的导航控制器成为自定义视图的代表,我尝试在导航控制器的视图中添加一个轻击手势识别器。没有任何效果。非常感谢任何建议或反馈。
谢谢!
我的自定义导航控制器:
class MainNavVC: UINavigationController {
var loadStatus = LoadStatus()
override func viewDidLoad() {
super.viewDidLoad()
// load status
loadStatus.bounds = CGRect(x: -24, y: -6, width: 0, height: 0)
let loadStatusButton = UIBarButtonItem(customView: loadStatus)
self.viewControllers.last?.navigationItem.leftBarButtonItem = loadStatusButton
let tap = UITapGestureRecognizer(target: self, action: #selector(loadStatusPressed))
loadStatus.addGestureRecognizer(tap)
loadStatus.isUserInteractionEnabled = true
}
@objc func loadStatusPressed(recognizer: UIGestureRecognizer) {
print("tapped")
let alert = UIAlertController(title: "Load Status", message: "When you are under a load, you are being tracked by a shipper.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
}
我的自定义视图:
class LoadStatus: UIView {
@IBOutlet var contentView: UIView!
private func commonInit(){
Bundle.main.loadNibNamed("LoadStatus", owner: self, options: nil)
addSubview(contentView)
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder ) {
super.init(coder: aDecoder)
commonInit()
}
}
【问题讨论】:
-
你想在你的所有 viewControllers 中使用这个自定义视图吗?
-
不,我只想要当前的。
标签: ios swift uiview uinavigationcontroller uitapgesturerecognizer