2015-12-21 74 views
0

时根本不会调用选择器我一直在努力解决这个问题很多天,希望有人能够帮助我解决这个问题。从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通过下面的代码创建我创建的笔尖(它非常简单,只是一个按钮)

enter image description here

背景已更改为指示按钮是在容器边界内创建的。

enter image description here

+0

更改UIView和按钮的背景颜色,以查看该按钮是否在容器视图边界内绘制,否则UIView将不会招待按钮的触摸。 – NeverHopeless

+0

@ NeverHopeless,我改变了它并确认按钮在容器视图范围内绘制。 – user3431933

+0

我不知道为什么,但在我的项目中,我尝试添加一个按钮,它不会调用选择器,直到我将'@ objc'添加到函数签名。不知道这是否会有帮助,或者为什么它对我有用。 – hannad

回答

0

我终于找到了答案,该代码波纹管在viewDidLoad中写了这个问题。绘制视图后,所创建的局部变量不再存在。

let favoriteIcon = mFavorite(sourceViewController: self, favoriteName: FavoriteNames.Parking, frame: CGRect(x: 265, y: 118, width: favoriteFrame.width, height: favoriteFrame.height)) 

我提出的解决方案是以下内容:

  1. 以我ViewControllerBase(从UIViewController中继承)中,i持有

    var favorite: mFavorite? 
    
  2. 在该衍生物类(从ViewControllerBase继承),我用以下内容初始化它:

    favoriteIcon = mFavorite(sourceViewController: self, favoriteName: FavoriteNames.Parking, frame: CGRect(x: 265, y: 118, width: favoriteFrame.width, height: favoriteFrame.height)) 
    

这个问题可能听起来有点愚蠢,但希望它能为某些遇到类似问题的人提供提示。

1

这通常是因为你的按钮是真实被拉过去其边界视图层次里面,然后使触摸只是内部的意见边界抓获。如果将剪辑蒙版添加到视图层次结构中,并且没有出现按钮,则父视图的框架中可能存在错误,可能是在sourceViewController的视图中。

尝试在你的setup函数的末尾添加以下代码:

// recursively clip all the ancestor views to their bounds 
var aView = view 
while aView != nil { 
    aView.clipsToBounds = true 
    aView = aView.superview 
}