我正在开发一个项目,其中包含一个"创建新帐户"视图控制器及其随附的名为"CreateNewAccount"的 Swift 类。用户可以在此视图控制器中放置 4 个输入值,即名字、姓氏、用户名和密码。单击此 VC 中的"创建帐户"按钮后,4 个输入值将传递给一个名为 UserInfoRetrieveModel 的 Swift 类(我相信在 MVC 的模型层内(,它们应该存储在那里。
然后,我想将这些值传递给另一个名为 UserInfoModel 的 Swift 类(也是一个模型(,然后它将第一个名称值委托给位于名为"ThanksForJoining"(及其随附类(的 VC 中的标签文本值。
我已经弄清楚了如何将值从VC传递到模型(CreateNewAccount到UserInfoRetrieveModel(以及从模型传递到VC(UserInfoModel到ThanksForJoining(,但是在我从模型转移到模型(UserInfoRetrieveModel到UserInfoModel(的某个地方,最初输入到"CreateNewAccount"中的值,我想将其传递给第二个模型类UserInfoModel变为nil。
以下是CreateNewAccount,UserInfoRetrieve,UserInfo和ThanksForJoining的代码:
创建新帐户 ->
import UIKit
class CreateNewAccount: UIViewController{
@IBOutlet weak var FNInput: UITextField!
@IBOutlet weak var LNInput: UITextField!
@IBOutlet weak var usernameInput: UITextField!
@IBOutlet weak var passwordInput: UITextField!
var uInfoRetrieve = UInfoRetrieveModel()
@IBAction func thanksForJoining(_ sender: Any) {
uInfoRetrieve.firstName = FNInput.text!
uInfoRetrieve.lastName = LNInput.text!
uInfoRetrieve.userName = usernameInput.text!
uInfoRetrieve.password = passwordInput.text!
uInfoRetrieve.delegate = self
uInfoRetrieve.retrieving()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension CreateNewAccount: UInfoRetrieveModelDelegate{
func credentialTransfer(data: String) {
print(data)
}
}
用户信息检索 ->
import Foundation
protocol UInfoRetrieveModelDelegate: class {
func credentialTransfer(data:String)
}
class UInfoRetrieveModel: NSObject {
weak var delegate: UInfoRetrieveModelDelegate?
var firstName: String = ""
var lastName: String = ""
var userName: String = ""
var password: String = ""
func retrieving(){
delegate?.credentialTransfer(data: firstName)
delegate?.credentialTransfer(data: lastName)
delegate?.credentialTransfer(data: userName)
delegate?.credentialTransfer(data: password)
}
}
用户信息 ->
import Foundation
protocol UserInfoModelDelegate: class {
func didReceiveDataUpdate(data: String)
}
class UserInfoModel {
weak var delegate: UserInfoModelDelegate?
let uInfoRetrieve = UInfoRetrieveModel()
func requestData() -> Array<String> {
let firstName = uInfoRetrieve.firstName
let lastName = uInfoRetrieve.lastName
let userName = uInfoRetrieve.userName
let password = uInfoRetrieve.password
delegate?.didReceiveDataUpdate(data: firstName)
delegate?.didReceiveDataUpdate(data: lastName)
delegate?.didReceiveDataUpdate(data: userName)
delegate?.didReceiveDataUpdate(data: password)
let credentials = [firstName, lastName, userName, password] as [Any]
return credentials as! Array<String>
}
}
谢谢加入 ->
import UIKit
class ThanksForJoining: UIViewController {
var userInfo = UserInfoModel()
@IBOutlet weak var firstName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
userInfo.delegate = self
firstName.text = userInfo.requestData()[0]
print("yo")
print(userInfo.requestData()[0])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ThanksForJoining: UserInfoModelDelegate {
func didReceiveDataUpdate(data: String) {
print(data)
}
}
UserInfoModel
和CreateNewAccount
都会创建一个新的UInfoRetrieveModel
实例。您必须正确连接它们才能传递信息。
正确连接意味着(以最简单的形式(一个构造另一个并将自己设置为另一个的委托,因此UInfoRetrieveModel
可以传递数据。子模型的构建通常通过computed property
完成。
例
struct Account {
let firstName: String, lastName: String
let userName: String, password: String
}
extension UInfoRetrieveModelDelegate: class {
createAccount(_ account: Account): Bool
}
extension UserInfoModel: UInfoRetrieveModelDelegate{
func createAccount(_ account: Account) -> Bool {
// Handling creation of account.
return success == true
}
var newUInfoRetrieveModel: UInfoRetrieveModel {
let helperModel = UInfoRetrieveModel(parent: self)
helperModel.delegate = self
return helperModel
}
}
解释
是的。你通常有一个Model
,你的数据,然后有一些东西可以控制对它的访问以对你的模型进行更改,管理模型的存储方式,也许与云服务同步,这是你在ViewControllers
之间传递ModelController
,更多/其他controllers
你通常使用,以防万一,使事情更简单。在您的情况下,您可能会将createAccount
(呼叫(传递给负责告诉modelController
创建帐户的controller/viewController
,然后告诉其views/viewControllers
之一显示模态/任何内容。
将数据传递到更高级别的常用方法是为viewController/controller
提供一个委托,它用来与上级进行通信,即"负责ViewController/controller
无法自行执行的操作"的委托,例如,如果给它一个modelController
是没有意义的,则向上推送数据(创建调用、修改调用、删除调用(,因为它不控制应用程序的该部分, 等。在您的情况下,您当然可以将modelController
传递给每个小viewController/view
,但通常更实用/更简单的是只将其提供给控制该部分的人,并让其他人与当前控制该部分的controller/viewController
进行通信。
这里更具体的意思是您可能不希望CreateAccountViewController
显示成功对话框,而是显示另一个,然后CreateAccountViewController
可以自己不这样做,因为它不再在堆栈上。