Мне нужна помощь с моим кодом. У меня есть UITextField
в UICollectionViewCell
. И я хочу получить доступ к данным моего UICollectionViewController
. Возвращаемое значение nil
.
Я использую метод делегата для передачи данных между классами, но мое значение возвращает nil
. Я не вижу, где проблема? Надеюсь, вы, ребята, можете мне помочь. Спасибо!
import UIKit
import Firebase
class EditUserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate {
weak var editUserProfileCellDelegate: EditUserProfileCell?
let cellId = "cellId"
super.viewDidLoad()
setupSaveButton()
setupNavigationBar()
collectionView?.register(EditUserProfileCell.self, forCellWithReuseIdentifier: cellId)
}
fileprivate func setupSaveButton() {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .plain, target: self, action: #selector(handleUpdateProfile))
}
@objc func handleUpdateProfile() {
editUserProfileCellDelegate?.delegate = self
guard let name = editUserProfileCellDelegate?.nameTextfield.text, name.count > 0 else { return }
guard let username = editUserProfileCellDelegate?.usernameTextfield.text, username.count > 0 else { return }
guard let email = editUserProfileCellDelegate?.emailTextfield.text, username.count > 0 else { return }
guard let city = editUserProfileCellDelegate?.cityTextfield.text else { return }
guard let country = editUserProfileCellDelegate?.countryTextfield.text else { return }
guard let fcmToken = Messaging.messaging().fcmToken else { return }
guard let uid = Auth.auth().currentUser?.uid else { return }
let ref = Database.database().reference().child("users")
let disctionaryValues = ["username": username, "name": name, "email": email, "city": city, "country": country,"fcmToken": fcmToken]
let values = [uid: disctionaryValues]
ref.setValue(values) { (err, ref) in
if let err = err {
print("Failed to update user:", err)
return
}
print("Succesfully updated user: ", self.user?.username ?? "")
}
}
И мой UITextField:
import UIKit
import Firebase
class EditUserProfileCell: UICollectionViewCell,
UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let nameTextfield: UITextField = {
let tf = UITextField()
let attributedText = NSMutableAttributedString(string: "Name",
attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.black])
tf.attributedPlaceholder = attributedText
tf.textColor = UIColor.black
tf.font = UIFont.systemFont(ofSize: 14)
tf.borderStyle = .none
tf.layer.backgroundColor = UIColor.white.cgColor
tf.layer.masksToBounds = false
tf.layer.shadowColor = UIColor.lightGray.cgColor
tf.layer.shadowOffset = CGSize(width: 0.0, height: 0.5)
tf.layer.shadowOpacity = 1.0
tf.layer.shadowRadius = 0.0
return tf
}()
Я ожидаю, что вернет ввод в моем текстовом поле, например, имя. Но теперь я могу получить только nil
.