したがって、問題は私のprofileViewControllerにあるようです。私はこのコードスニペットにサインアップした後、現在のユーザーを設定します。コントローラがアプリケーションに行く前に初期化されています
class func setCurrent(_ user: User, writeToUserDefaults: Bool = true) {
print(user)
print("")
if writeToUserDefaults {
let data = NSKeyedArchiver.archivedData(withRootObject: user)
UserDefaults.standard.set(data, forKey: "currentUser")
UserDefaults.standard.synchronize()
}
_current = user
print(_current)
}
Then Afterそれは私のprofileViewControllerに行き、nullであることが判明したユーザを探すことを試みます。私がviewcontrollerに行く前に、なぜそこに行くのでしょうか?なぜそれは無名ですか?以下 は、私がsetCurrentを呼び出すメソッドは、このメソッドはhomeviewControllerにユーザーを移動し、ルートとして設定するために呼び出され
// will handle the sign up of a user
@objc func handleSignUp(){
// first we cant to take sure that all of the fields are filled
var profilePic: String = ""
// will take the user selected image and load it to firebase
let imageName = NSUUID().uuidString
guard let username = self.nameTextField.text,
let confirmPassword = self.confirmPasswordTextField.text,
let email = self.emailTextField.text,
let password = self.passwordTextField.text,
!username.isEmpty,
!email.isEmpty,
!password.isEmpty,
!confirmPassword.isEmpty
else {
print("Required fields are not all filled!")
return
}
if self.validateEmail(enteredEmail:email) != true{
let alertController = UIAlertController(title: "Error", message: "Please Enter A Valid Email", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
// will make sure user is validated before it even tries to create user
// will make sure the password and confirm password textfields have the same value if so it will print an error
if self.passwordTextField.text != self.confirmPasswordTextField.text {
let alertController = UIAlertController(title: "Error", message: "Passwords Don't Match", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
//create a reference to the sotrage database in firebase
let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName).PNG")
//following function does the work of putting it into firebase
//notice I also set the value of profilepic oo it can be saved in the updated user instance in the database
if let userImage = selectedImageFromPicker,let uploadData = UIImageJPEGRepresentation(userImage, 0.1){
AuthService.createUser(controller: self, email: email, password: password) { (authUser) in
guard let firUser = authUser else{
return
}
storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
if error != nil {
print(error ?? "")
return
}
profilePic = (metadata?.downloadURL()!.absoluteString)!
//printing to make sure values are contained in these strings
print(profilePic)
print(username)
UserService.create(firUser, username: username, profilePic: profilePic, location: self.userLocation!, completion: { (user) in
guard let user = user else {
print("User not loaded into firebase db")
return
}
User.setCurrent(user, writeToUserDefaults: true)
// will set the current user for userdefaults to work
print(user.profilePic ?? "")
print(user.username ?? "")
// self.delegate?.finishSigningUp()
self.finishSigningUp()
})
})
}
}
}
を下回っている私のプロフィールビューコントローラと私のviewDidLoad
class ProfileeViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var profileHandle: DatabaseHandle = 0
var profileRef: DatabaseReference?
let cellID = "cellID"
let profileSetupTransition = AlterProfileViewController()
let settingView = SettingsViewController()
var userEvents = [Event]()
var userId: String?
var user: User?
var emptyLabel: UILabel?
var currentUserName: String = ""
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.white
let user = self.user ?? User.current
profileHandle = UserService.observeProfile(for: user) { [unowned self](ref, user, events) in
self.profileRef = ref
self.user = user
self.userEvents = events
// self.jobs = allJobs
// self.reciepts = allReciepts
// print(self.userEvents)
// print(self.reciepts)
DispatchQueue.main.async {
self.collectionView?.reloadData()
}
}
ある
func finishSigningUp() {
print("Finish signing up from signup view controller")
print("Attempting to return to root view controller")
let homeController = HomeViewController()
//should change the root view controller to the homecontroller when done signing up
self.view.window?.rootViewController = homeController
self.view.window?.makeKeyAndVisible()
}
「ビューコントロールに行く前にどうしてそんなに行くの?」もっと説明できますか? – 3stud1ant3
表示されている 'ProfileeViewController'に関して' setCurrent'をどこで呼びますか? – Paulw11
サインアップビューコントローラーにサインアップした後、何らかの理由でsetsCurrentを設定すると、私はプロファイルにあるユーザー変数に直接アクセスします。viewController @ Paulw11 –