ProfileEditViewController

ProfileEditViewController

嘿,我是编程新手,我的问题是,我有一个带有4个可水平滚动单元格的UICollectionViewController。在第四个细胞内,我有一个UIButtonoptionsButton)在一个UIViewProfileContainerView)上面。
我要呈现的UIViewController称为ProfileEditViewController,并在Main.storyboard中设置。
按下此按钮后如何显示UIViewController
剖面单元格:

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")
    }
}

家庭视图控制器:
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,它的样式如下:
1)给你的Main.storyboardaProfileEditViewController。例如,“ProfileEditViewController”-这里有一些关于此的问题:What is a StoryBoard ID and how can i use this?
2)为StoryBoard ID上的操作注册UIViewController或提供适当的回调功能。
由于UIButton也是Collection视图的数据源,因此可以轻松地扩展datasource方法

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中,您需要提供一个功能来支持使用所需的故事板ID转换到该特定控制器:
let storyboard = UIStoryboard(name: "Main", bundle:Bundle.main)
let controller = storyboard.instantiateViewController(withIdentifier: "ProfileEditViewController")
self.present(controller, animated: true, completion: nil)

关于ios - 如何在按下UICollectionViewCell内部的按钮后呈现ViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46828578/

10-13 04:17