我想要做的解释:
同样在“SignUpVC”里面我想要一个协议来保存我的值 params 和“保存”它们可供稍后在其他ViewControllers中使用。不使用NSUserDefaults。
现在保存参数并将其分配给协议 函数的参数,我想进入另一个调用的ViewController “ SchoolSelectionVC ”并调出这些值并指定 params 到 var newParams:[String:AnyObject] 所以它们相同,但是如果不能改变某些值, 我想......
所以基本上,我试图用SignUpVC的用户输入来分配一些值,保存这些值,并将它们带到另一个ViewController中的另一个变量中以供进一步使用或更改。
问题:
下面是我的代码,它显示我正在创建一个 SignUpVC协议,一个名为“ var signupDelegate = SignUpVCProtocol ”的代表,一个 let param < / strong>,分配值,尝试将 let param 分配给 SignUpVC协议参数,然后转移到另一个ViewController(“ SchoolSelectionVC ” ),但错误在那里停止。这一切都发生在 SignUpVC 内的按钮点击操作中。
我只是想知道为什么持有SignUpVCProtocol的委托变量及其功能在我尝试将参数传递给它时会给我一个错误。
如果有关于如何将值从一个VC保存到另一个VC的任何提示,请告诉我,如果我的任何代码需要更多解释,请告诉我!
代码:
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
在按钮操作中:
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]
}
}
答案 0 :(得分:0)
不要使用协议在viewControllers之间传递数据,请使用
func prepare(对于segue:UIStoryboardSegue,发件人:Any?)
代替。
检查这个答案: Passing object with prepareForSegue Swift
对于viewControllers,您需要以编程方式加载,您可以使用此示例传递数据:
//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)