Ios 如何在按下CollectionViewCell内的按钮后显示ViewController
嘿,我是编程新手,我的问题是,我有一个Ios 如何在按下CollectionViewCell内的按钮后显示ViewController,ios,iphone,swift,uicollectionview,presentviewcontroller,Ios,Iphone,Swift,Uicollectionview,Presentviewcontroller,嘿,我是编程新手,我的问题是,我有一个UICollectionViewController,其中有4个单元格可以水平滚动。在第四个单元格内,我在ui视图(ProfileContainerView)上方有一个ui按钮(options按钮) 我要演示的UIViewController称为ProfileEditViewController,它是在Main.storyboard中设置的 按下此按钮后,如何显示UIViewController ProfileCell: class ProfileCell:
UICollectionViewController
,其中有4个单元格可以水平滚动。在第四个单元格内,我在ui视图
(ProfileContainerView
)上方有一个ui按钮(options按钮
)
我要演示的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
}
}
您可以通过以下方式显示您的ProfileEditViewController
,该控制器在Main.storyboard
中设置了样式:
1) 为您的ProfileEditViewController
提供情节提要ID
。例如,“ProfileEditViewController”-这里有一些与此相关的问题:
2) 为ui按钮上的操作注册ui视图控制器
,或提供适当的回调功能。
由于您的HomeViewController
也是集合视图的数据源,因此您可以轻松地扩展数据源方法
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell`
实施过程可能如下所示:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 3 {
let profileCell = collectionView.dequeueReusableCell(withReuseIdentifier: profileCelId, for: indexPath)
if let _cell = cell as? ProfileCell,
let button = _cell.optionsButton as? UIButton {
button.addTarget(self, action: #selector(handleOptionsButton), forControlEvents: UIControlEvents.TouchUpInside)
}
return profileCell;
}
return cell
}
确保按钮操作现在也由您的HomeViewController实施
@objc func handleOptionsButton() {
print("Button pressed")
}
3) 现在,在HomeViewController.handleOptionsButton
中,您需要提供一个功能,以支持使用所需情节提要ID转换到特定控制器:
let storyboard = UIStoryboard(name: "Main", bundle:Bundle.main)
let controller = storyboard.instantiateViewController(withIdentifier: "ProfileEditViewController")
self.present(controller, animated: true, completion: nil)
您可以使用委托来实现这一点
下面是实现此功能的代码
protocol ProfileCollectionViewCellDelegate {
func buttonPressedAtIndexPath(inCell: ProfileCell)
}
class ProfileCell: UICollectionViewCell {
var delegate : ProfileCollectionViewCellDelegate?
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() {
if self.delegate != nil {
self.delegate?.buttonPressedAtIndexPath(self)
}
}
}
对于您的HomeViewController
class HomeViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, ProfileCollectionViewCellDelegate {
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 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: profileCelId, for: indexPath)
cell.delegate = self
return cell
}
fun buttonPressedAtIndexPath(inCell: ProfileCell) {
let indexOfCell = self.collectionView.indexPath(for: cell)
if indexOfCell.row == 3 {
//Do your work here
}
}
}
设proCell=ProfileCell()
检查此项:我尝试过,但它显示“ProfileCell”类型的值没有成员“present”present是由UIViewControllers实现的函数参见此处:。例如,您可以注册UIViewController以执行Uibutton的操作。我会调整我的答案。好的,我会试试这个,然后让你知道它是否有效。如果let button=profileCell.options按钮为,谢谢您的帮助?UIButton{button.addTarget(self,action:#选择器(handleOptionsButton),forControlEvents:UIControlEvents.TouchUpInside)}哦,是的,我收到了,学到了很多,谢谢。如果你想知道我做了什么,我加了一条评论。谢谢你的回答,我会试试的。弗兰克,你试过了吗?对不起,我没有注意到你写信给我是的,我试过了。但不幸的是,这对我不起作用。但无论如何,谢谢你的回答和时间!出了什么问题?
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cell, for: indexPath)
if indexPath.item == 3 {
let profileCell = collectionView.dequeueReusableCell(withReuseIdentifier: profileCelId, for: indexPath)
let button = proCell.optionsButton
button.addTarget(self, action: #selector(handleOptionsButton), for: UIControlEvents.touchUpInside)
return profileCell;
}
return cell
}