嗨,我是新手编程和我的问题是,我有一个UICollectionViewController
与4个单元格可水平滚动。在第四个单元格内,我在UIView
(ProfileContainerView
)的顶部有一个UIButton
(optionsButton
)。如何在按下CollectionViewCell中的按钮后显示ViewController
我想提出的UIViewController
被称为ProfileEditViewController
并设置在Main.storyboard
。
如何在按下此按钮后显示UIViewController
?
ProfileCell:
class ProfileCell: UICollectionViewCell {
let profileContainerView: UIView = {
let view = UIView()
return view
}()
lazy var optionsButton: UIButton = {
let btn = UIButton(type: .custom)
btn.setImage(#imageLiteral(resourceName: "Settings"), for: UIControlState.normal)
btn.addTarget(self, action: #selector(handleOptionsButton), for: UIControlEvents.touchUpInside)
return btn
}()
@objc func handleOptionsButton() {
print("Button pressed")
}
}
HomeViewController:
class HomeViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let profileCelId = "profileCell"
override func viewDidLoad() {
super.viewDidLoad()
setupSwipeView()
}
func setupSwipeView() {
collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cell)
collectionView?.register(ProfileCell.self, forCellWithReuseIdentifier: profileCelId)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 3 {
return collectionView.dequeueReusableCell(withReuseIdentifier: profileCelId, for: indexPath)
}
return cell
}
}
检查该代码:https://stackoverflow.com/questions/43766963/swift-action-button-inside-a-collectionviewcell – Amit