从集合视图控制器调用集合视图标题中的UIButton时不起作用,如何解决此问题?

时间:2019-07-08 22:16:44

标签: ios swift

我有一个包含按钮的UICollectionViewCell文件。当我单击此按钮时,我想将按钮标签从“关注”更改为“取消关注”(例如instagram),并使用协议和委托在UICollectionView Controller swift文件中的单击按钮函数上调用此按钮。但是按钮似乎没有响应。

我在控制器中也添加了UICollectionViewCell作为SupplementalViewofKind,但是仍然存在问题。还要确保我遵守委托协议

// ********* VIEW FILE (UserProfileHeader.swift) *********
class UserProfileHeader: UICollectionViewCell {

var delegate: UserProfileHeaderDelegate?

let editProfileFollowButton: UIButton = {
 let button = UIButton(type: .system)
 button.setTitle("Follow", for: .normal)
 button.addTarget(self, action: #selector(handleEditProfileFollow), 
 for: .touchUpInside)
 return button
    }()

override init(frame: CGRect) {
  super.init(frame: frame)

  addSubview(editProfileFollowButton)

    }

@objc func handleEditProfileFollow(){
        delegate?.handleEditFollowTapped(for: self)
    }

}

// ********* CONTROLLER FILE (UserProfileVC.swift) *********
class UserProfileVC: UICollectionViewController, 
UICollectionViewDelegateFlowLayout, UserProfileHeaderDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    //register collection views
    self.collectionView!.register(UICollectionViewCell.self, 
    forCellWithReuseIdentifier: reuseIdentifier)

    self.collectionView.register(UserProfileHeader.self, 
    forSupplementaryViewOfKind: 
    UICollectionView.elementKindSectionHeader, withReuseIdentifier: 
    headerIdentifier)

}


override func numberOfSections(in collectionView: UICollectionView) 
-> Int {
    return 1
}


override func collectionView(_ collectionView: UICollectionView, 
numberOfItemsInSection section: Int) -> Int {
    return 0
}

// declaring the header collection view
override func collectionView(_ collectionView: UICollectionView, 
viewForSupplementaryElementOfKind kind: String, at indexPath: 
IndexPath) -> UICollectionReusableView {
    let header = 
      collectionView.dequeueReusableSupplementaryView(ofKind: kind, 
      withReuseIdentifier: headerIdentifier, for: indexPath) as! 
      UserProfileHeader
    header.delegate = self
    return header
}

func collectionView(_ collectionView: UICollectionView, layout 
collectionViewLayout: UICollectionViewLayout, 
referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: view.frame.width, height: 220)
}

override func collectionView(_ collectionView: UICollectionView, 
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = 
      collectionView.dequeueReusableCell(withReuseIdentifier: 
      reuseIdentifier, for: indexPath)
    return cell
}


func handleEditFollowTapped(for header: UserProfileHeader){
    if header.editProfileFollowButton.titleLabel?.text == "Follow"{
        header.editProfileFollowButton.setTitle("Following", for: 
          .normal)
    } else {
        header.editProfileFollowButton.setTitle("Follow", for: 
          .normal)
    }
}

}

// ********* PROTOCOL FILE (protocols.swift) *********
protocol UserProfileHeaderDelegate {
 func handleEditFollowTapped(for header: UserProfileHeader)
}

1 个答案:

答案 0 :(得分:0)

我看到的问题是您不能在anonymous closure中添加目标,原因是创建按钮时self不存在。

对此的简单解决方法是删除按钮创建中的下一行:

button.addTarget(self, action: #selector(handleEditProfileFollow), for: .touchUpInside)

init中,只需在super.init之后添加以下行:

editProfileFollowButton.addTarget(self, action: #selector(handleEditProfileFollow), for: .touchUpInside)

希望这对您有帮助