我想在自定义集合视图单元格内的按钮目标函数中导航新的视图控制器
import UIKit
class ItemDetailsHeaderCell: UICollectionViewCell {
let likeImage : UIButton = {
let btn = UIButton()
btn.setImage(#imageLiteral(resourceName: "like").withRenderingMode(.alwaysTemplate), for: [])
btn.tintColor = main_color
return btn
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(likeImage)
// anchor function to put constrains
likeImage.anchor(bottomAnchor, left: leftAnchor, bottom:
bottomAnchor, right: rightAnchor, topConstant: 0, leftConstant: 0,
bottomConstant: 0, rightConstant: 0, widthConstant: 0,
heightConstant: 50)
likeImage.addTarget(self, action: #selector(likeWeb), for:
.touchDown)
}
@objc func likeWeb() {
// here it is not work
let controller = ItemDetailsViewController(collectionViewLayout:
UICollectionViewFlowLayout())
controller.itemid = String( indexPath.row )
navigationController?.pushViewController(controller, animated:
true)
}
}
注意:我仅使用swift代码,我的项目中没有任何存储板
答案 0 :(得分:1)
@objc func likeWeb() {
// here it is not work
let controller = self.storyboard?.instantiateViewControllerWithIdentifier("ItemDetailsViewControllerIdentifier") as! ItemDetailsViewController
controller.itemid = String( indexPath.row )
self.navigationController?.pushViewController(controller, animated:
true)
}
答案 1 :(得分:0)
let vc = TwoViewController(nibName: "TwoViewController",bundle: nil)
navigationController?.pushViewController(vc,animated: true)
答案 2 :(得分:0)
更改为“惰性变量”,而不是“ let”
lazy var likeImage : UIButton = {
let btn = UIButton()
btn.setImage(#imageLiteral(resourceName: "like").withRenderingMode(.alwaysTemplate), for: [])
btn.tintColor = main_color
return btn
}()