Reputation: 203
Explanation of what I want to do:
Also inside "SignUpVC" I want a protocol to hold my values for params and "save" them for later use in other ViewControllers. Without using NSUserDefaults.
Now with those params being saved and assigned to the protocol's function's parameters, I want to go into another ViewController called "SchoolSelectionVC" and bring up those values and assign the params to a var newParams : [String : AnyObject] so they equal the same thing but ill be able to change some of the values if I wanted to...
So basically, I am trying to assign some values with the user input from SignUpVC, save those values, and bring them over to another variable in another ViewController for further use or change.
Problem:
Below is my code, and it shows that i am creating a SignUpVC protocol, a delegate called "var signupDelegate = SignUpVCProtocol", a let param, assigning it values, trying to assign the let param to the SignUpVC Protocol parameters, then move into another ViewController ("SchoolSelectionVC"), but the error stops it there. This all happens inside a button click action inside SignUpVC.
I just want to know why the delegate variable holding the SignUpVCProtocol and its function is giving me an error when I try passing the parameters to it.
If there are any tips on how to save values from one VC to another, let me know and if any of my code needs more explanation let me know too!
Code:
protocol SignUpVCProtocol {
func logInData(params : [String : AnyObject])
}
class SignUpVC: UIViewController{
//MARK:- @IBOutlet
//MARK: ^^^^^^^^^^^^^^^^^^^^
@IBOutlet var signUpWithFacebookButton: UIButton!
//MARK:- Properties
//MARK: ^^^^^^^^^^^^^^^^^^^^
var selectedTextField : UITextField!
var keyboardPresent = false
var loginFlag = false
var fbId: String!
var picUrl: String!
var schoolID : String = ""
var schoolDict : [String:AnyObject]!
var player: AVPlayer?
var fbData : AnyObject!
let domainLbl = UILabel()
var signUpDelegate : SignUpVCProtocol! //Delegate var for Protocol
Inside a Button Action:
let params = [
"Action" : "signUp" as AnyObject,
"name" : self.nameTextField.text! as AnyObject,
"email" : "\(self.emailTextField.text!)" as AnyObject,
"password" : self.passwordTextField.text! as AnyObject,
"school" : self.schoolTextField.text! as AnyObject,
"device_token" : sharedAppdelegate.DeviceToken as AnyObject,
"device_type" : "iphone" as AnyObject,
"school_id" : self.schoolID as AnyObject
]
self.signUpDelegate.logInData(params: params) //!!Where i want to assign the above params to the protocol params for use in SchoolSelectionVC
//ERROR HAPPENS RIGHT ABOVE^^
let schoolVC = self.storyboard?.instantiateViewController(withIdentifier: "SchoolSelectionVC") as! SchoolSelectionVC
let navVC = UINavigationController(rootViewController: schoolVC)
navVC.isNavigationBarHidden = true
schoolVC.delegate = self
self.present(navVC, animated: true, completion: nil)
//in another VC Class
extension SchoolSelectionVC: SignUpVCProtocol
{
func logInData(params: [String : AnyObject]) {
self.newParams = params as! [String : String]
}
}
Upvotes: 0
Views: 433
Reputation: 3089
Don't use protocols to pass data between viewControllers, use
func prepare(for segue: UIStoryboardSegue, sender: Any?)
instead.
Check this answer: Passing object with prepareForSegue Swift
For viewControllers you need to load programmatically you can use this example of passing data:
//For using without navigation controller
let vc = SomeViewController(nibName: "SomeViewController", bundle: nil);
vc.someVariable = someData
self.present(vc, animated: true, completion: nil)
//For using with navigation controller
let vc = SomeViewController(nibName: "SoveViewController", bundle: nil);
vc.someVariable = someData
self.navigationController?.pushViewController(vc, animated: true)
Upvotes: 0