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

标签 swift model-view-controller uiviewcontroller model

我正在开发一个项目,其中包含一个“创建新帐户” View Controller 及其随附的名为“CreateNewAccount”的 Swift 类。用户可以将 4 个输入值放入此 View Controller 中,名字、姓氏、用户名和密码。单击此 VC 中的“创建帐户”按钮后,4 个输入值将传递到一个名为 UserInfoRetrieveModel 的 Swift 类(我相信是在 MVC 的模型层中),它们应该存储在该类中。

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

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

下面是 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)
   }
}

最佳答案

UserInfoModelCreateNewAccount 都创建了一个新的 UInfoRetrieveModel 实例。您必须正确连接它们,它们才能传递信息。

正确连接意味着(以最简单的形式)一个构造另一个并将自己设置为另一个的委托(delegate),因此 UInfoRetrieveModel 可以传递数据。子模型的构造通常是通过计算属性完成的。

例子

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 你通常使用 incase 让事情变得更简单。在您的情况下,您可能会将 createAccount(调用)传递给 controller/viewController,它负责告诉 modelController 创建帐户,然后告诉其中一个 views/viewControllers 显示模式/无论什么。

将数据传递到更高级别的通常方法是为 viewController/controller 提供一个用于与更高级别通信的委托(delegate),该委托(delegate)“负责 ViewController/controller 不能自己做”,例如,如果给它一个 modelController 是没有意义的,那么向上推送数据(创建调用、修改调用、删除调用),因为它不控制那部分应用程序等。在您的情况下,您当然可以将 modelController 传递给每个小 viewController/view,但通常更实用/更简单的是只将它传递给一个控制该部分并让其他人与当前控制该部分的 controller/viewController 进行通信。

这里更具体意味着您可能不希望 CreateAccountViewController 显示成功对话框,而是另一个,CreateAccountViewController 可以不自己做,因为它不在再堆叠。

关于swift - 如何在 Swift 中将数据从一个模型传递到另一个模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51970814/

相关文章:

swift - AVSpeechSynthesizer isSpeaking 在 Swift 中不起作用

iOS - Firebase Storage 尝试在 didFinishLaunchingWithOptions 之前初始化

ios - 更改 View Controller

swift - 在 Swift 中,如何使类的对象数组只能作为整个数组访问?

swift - 从集合中引用文本标签查看项目

.net - 如何在布局 View 中使用 dbcontext(在 .NET MVC、 Entity Framework 中)

javascript - 如何在 Web 应用程序中采用 MVC?

asp.net-mvc - 如何在 MVC 运行时自定义 Display 和 Required 属性

ios - 在iOS6/7上未调用willAnimateRotationToInterfaceOrientation

iphone - 将 UIViewController 转换为 UIScrollViewController