Wordle Game - Additional Delete Button Error

46 Views Asked by At

I am currently developing a wordle game and I'm facing difficulties while attempting to implement a delete button feature. As a beginner in coding, I am seeking assistance to identify and resolve the error in my code. The project consists of multiple files, but I would like to focus on the specific issue within the KeyboardViewController file for now.

At the bottom of the KeyboardViewController file, I encountered an unexpected error message: 'Cannot find type 'View' in scope.' This error message suggests that there is an issue related to the recognition of the View type within the current scope. It is worth noting that the project utilizes SwiftUI for the user interface design.

To provide a more thorough solution, I would appreciate guidance on how to resolve this error. Additionally, if there are any specific code snippets or additional error messages that would be helpful in diagnosing the issue, please let me know. Thank you in advance for your assistance! Keyboard View Controller File:

I was trying to use constraints and build a delete button for my wordle keyboard view controller; however, I don't understrand the issue and its error. Can anyone help me out and make a refined version which works and any other tips.

The Keyboard View Controller Code:

import UIKit

protocol KeyboardViewControllerDelegate: AnyObject {
    func keyboardViewController(
        _ vc: KeyboardViewController,
        didTapKey letter: Character
    )
}

class KeyboardViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
   
    var selectedLetter: Character?
    
    var previousKey: Character? // Declare the previousKey property
    
    weak var delegate: KeyboardViewControllerDelegate?

    let letters = ["qwertyuiop", "asdfghjkl", "zxcvbnm"]
    private var keys: [[Character]] = []
    

    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 2
        let collectionVIew = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionVIew.translatesAutoresizingMaskIntoConstraints = false
        collectionVIew.backgroundColor = .clear
        collectionVIew.register(KeyCell.self, forCellWithReuseIdentifier: KeyCell.identifier)
        return collectionVIew
    }()


    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self
        view.addSubview(collectionView)
        NSLayoutConstraint.activate([
            collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 30),
            collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])

        for row in letters {
            let chars = Array(row)
            keys.append(chars)
        }
    }
}

extension KeyboardViewController {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return keys.count
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return keys[section].count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyCell.identifier, for: indexPath) as? KeyCell else {
            fatalError()
        }
        let letter = keys[indexPath.section][indexPath.row]
        cell.configure(with: letter)
        return cell
    }
    
    class MyViewController: UIViewController {
        var previousKey: KeyboardViewController?
        var selectedLetter: Character?
        // ...
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let margin: CGFloat = 20
        let size: CGFloat = (collectionView.frame.size.width-margin)/10

        return CGSize(width: size, height: size*1.5)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

        var left: CGFloat = 1
        var right: CGFloat = 1

        let margin: CGFloat = 20
        let size: CGFloat = (collectionView.frame.size.width-margin)/10
        let count: CGFloat = CGFloat(collectionView.numberOfItems(inSection: section))

        let inset: CGFloat = (collectionView.frame.size.width - (size * count) - (2 * count))/2

        left = inset
        right = inset

        return UIEdgeInsets(
            top: 2,
            left: left,
            bottom: 2,
            right: right
        )
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        let letter = keys[indexPath.section][indexPath.row]
        previousKey?.selectedLetter = letter // Save the selected letter to previousKey
        delegate?.keyboardViewController(self, didTapKey: letter)
    }
    
    func setupUI() {
        let button = UIButton(type: .system)
        button.setImage(UIImage(systemName: "delete.backward.fill"), for: .normal)
        button.imageView?.contentMode = .scaleAspectFit
        button.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .heavy)
        button.frame = CGRect(x: 0, y: 0, width: 40, height: 50)
        button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        button.isEnabled = previousKey?.currentWord.count != 0 && selectedLetter.inPlay
        button.alpha = button.isEnabled ? 1.0 : 0.4
        
        self.view.addSubview(button)
    }
    
    @objc func buttonTapped() {
        previousKey?.letters // Modify this line based on the appropriate function or method
    }
}

class MyViewController: UIViewController {
    var previousKey: KeyboardViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    func setupUI() {
        let button = UIButton(type: .system)
        button.setImage(UIImage(systemName: "delete.backward.fill"), for: .normal)
        button.imageView?.contentMode = .scaleAspectFit
        button.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .heavy)
        button.frame = CGRect(x: 0, y: 0, width: 40, height: 50)
        button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        button.isEnabled = previousKey?.currentWord.count != 0
        button.alpha = button.isEnabled ? 1.0 : 0.4
        
        self.view.addSubview(button)
    }
    
    @objc func buttonTapped() {
        previousKey?.removeLetterFromCurrentWord()
    }
}
0

There are 0 best solutions below