如何在Swift中将数据从一个模型传递到另一个模型?

时间:2018-08-22 15:58:54

标签: swift model-view-controller uiviewcontroller model

我正在一个包含“创建新帐户”视图控制器及其随附的Swift类“ CreateNewAccount”的项目。用户可以将4个输入值,名字,姓氏,用户名和密码放入此视图控制器中。单击此VC中的“创建帐户”按钮后,这4个输入值将传递到一个名为UserInfoRetrieveModel的Swift类(我相信在MVC的模型层内)。

然后,我想将这些值传递给另一个名为UserInfoModel的Swift类(也就是一个模型),然后将其名字委托给位于名为“ ThanksForJoining”的VC中的label的文本值(及其伴随的类)。

我已经弄清楚了如何将值从VC传递到模型(CreateNewAccount到UserInfoRetrieveModel)以及从模型传递到VC(UserInfoModel到ThanksForJoining),但是在我从模型到模型的转换中(从UserInfoRetrieveModel到UserInfoModel)传递了最初输入的值我想传递给第二个模型类UserInfoModel的“ CreateNewAccount”变为nil。

下面是CreateNewAccount,UserInfoRetrieve,UserInfo和ThanksForJoining的代码:

CreateNewAccount->

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)
  }
}

UserInfoRetrieve->

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)
    }

}

UserInfo->

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)
   }
}

1 个答案:

答案 0 :(得分:1)

UserInfoModelCreateNewAccount都创建一个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,数据,然后有一些东西可以控制对它的访问以在模型上进行更改,管理模型的存储方式(也许与云服务同步),即ModelController您通常在ViewControllers和其他controllers之间传递,以防万一。在您的情况下,您可能会将createAccount(呼叫)传递到controller/viewController,负责告诉modelController创建帐户,然后告诉其中一个{{1} }以显示模态/任何形式。

将数据传递到更高级别的通常方法是让views/viewControllers与其用于与更高层进行通信的委托,即“负责viewController/controller本身无法执行的操作” ,例如,如果由于无法控制应用程序的那部分而没有必要赋予它ViewController/controller,则推升数据(创建调用,修改调用,删除调用)。在您的情况下,您当然可以通过每个小modelController都有一个modelController,但通常更实用/更简单的做法是只将其交给控制零件的人,而让其他人与当前控制该零件的viewController/view进行通信

这里更具体的意思是您可能不希望controller/viewController显示成功对话框,而是另一个CreateAccountViewController本身不能显示的对话框,因为它不再在堆栈中了。