时根本不会调用选择器我一直在努力解决这个问题很多天,希望有人能够帮助我解决这个问题。从Xib加载的UIView中的UIButton在您点击
我创建了一个名为“mFavorite”的UIView
,它继承自NSObject
。原因是视图将从nib加载并在从使用的ViewController
初始化时设置。
我可以看到它正在启动正确的,但是当我点击它只是从来没有所谓的选择favoriteTapped。
类mFavorite的是波纹管:
let favoriteIcon = mFavorite(sourceViewController: self, favoriteName: FavoriteNames.Parking, frame: CGRect(x: 265, y: 118, width: favoriteFrame.width, height: favoriteFrame.height))
favoriteIcon.delegate = self
我在这里也附:
import UIKit
let favoriteDataModel = FavoriteDataModel()
let favoriteFrame: CGRect = CGRect(x: 0, y: 0, width: 31, height: 29)
protocol mFavoriteDelegate: class{
func mFavoriteFavoriteIconDidPressed()
}
class mFavorite: NSObject{
var frame: CGRect
var view:UIView!
var favoriteName: String
var sourceViewController: ViewControllerBase?
weak var delegate: mFavoriteDelegate?
var isFavorite: Bool = false{
didSet{
updateImage()
updateFavoriteToDataModel()
}
}
init(sourceViewController: ViewControllerBase, favoriteName: String, frame: CGRect){
self.sourceViewController = sourceViewController
self.favoriteName = favoriteName
self.frame = frame
self.isFavorite = favoriteDataModel.isFavoriteExist(favoriteName)
super.init()
setup()
}
convenience init(sourceViewController: ViewControllerBase, favoriteName: String){
self.init(sourceViewController: sourceViewController, favoriteName: favoriteName, frame: favoriteFrame)
}
func setup() {
view = loadViewFromNib()
view.frame = frame
let btnfavorite = view.viewWithTag(ViewTags.btnFavorite) as! UIButton
btnfavorite.addTarget(self, action: "favoriteTapped:", forControlEvents: UIControlEvents.TouchDown)
//btnfavorite.userInteractionEnabled = true
sourceViewController?.view.addSubview(view)
let widthConstraints = NSLayoutConstraint(item: view, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: view.frame.size.width)
let heightConstraints = NSLayoutConstraint(item: view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: view.frame.size.height)
let topConstraints = NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal, toItem: sourceViewController?.view, attribute: .Top, multiplier: 1.0, constant: view.frame.origin.y)
let trailConstraints = NSLayoutConstraint(item: view, attribute: .Trailing , relatedBy: .Equal, toItem: sourceViewController?.view, attribute: .Leading, multiplier: 1.0, constant: screenWidth - 20)
view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activateConstraints([widthConstraints, heightConstraints, topConstraints, trailConstraints])
}
func loadViewFromNib() -> UIView {
//let bundle = NSBundle(forClass:self.dynamicType)
let nib = UINib(nibName: "mFavorite", bundle: nil)
let view = nib.instantiateWithOwner(nil, options: nil)[0] as! UIView
return view
}
func updateImage(){
let btnfavorite = view.subviews[0] as! UIButton
let img = isFavorite ? UIImage(named: "star-yellow") : UIImage(named: "star-grey")
btnfavorite.setBackgroundImage(img, forState: UIControlState.Normal)
}
func updateFavoriteToDataModel(){
if isFavorite{
favoriteDataModel.addToFavorite(favoriteName)
}else{
favoriteDataModel.removeFromFavorite(favoriteName)
}
}
func favoriteTapped(sender: UIButton){
isFavorite = !isFavorite
delegate!.mFavoriteFavoriteIconDidPressed()
}
}
有了这个mFavorite,然后我在其他UIViewController
通过下面的代码创建我创建的笔尖(它非常简单,只是一个按钮)
背景已更改为指示按钮是在容器边界内创建的。
更改UIView和按钮的背景颜色,以查看该按钮是否在容器视图边界内绘制,否则UIView将不会招待按钮的触摸。 – NeverHopeless
@ NeverHopeless,我改变了它并确认按钮在容器视图范围内绘制。 – user3431933
我不知道为什么,但在我的项目中,我尝试添加一个按钮,它不会调用选择器,直到我将'@ objc'添加到函数签名。不知道这是否会有帮助,或者为什么它对我有用。 – hannad