gpt4 book ai didi

swift - 如何快速将数据从 View Controller 传递到数据模型

转载 作者:可可西里 更新时间:2023-11-01 01:16:54 25 4
gpt4 key购买 nike

我正在构建一个与网络服务对话的简单应用程序。

我使用委托(delegate)方法来传递数据(从我的模型到 View Controller )。

但我不确定如何从我的模型中的 View Controller (text_field.text) 读取数据。我需要这样做,以便我可以将正确的参数传递给我的网络服务

我的 View Controller 是:

import UIKit

class ViewController: UIViewController,HomeModelDelegate {
var homeModel = HomeModel()

@IBOutlet weak var loginid: UITextField!
@IBOutlet weak var pwd: UITextField!

@IBAction func submit(_ sender: UIButton) {

homeModel.chkpwd()
//Here viewcontroller is assigning itself to the homemodel's delegate property
homeModel.delegate = self
}

override func viewDidLoad() {
super.viewDidLoad()
loginid.layer.cornerRadius=10
pwd.layer.cornerRadius = 10
}

func itemsDownloaded(locations: [Location]) {
loginid.text = locations[0].pwd
}
}

我的模型代码是:

import UIKit

protocol HomeModelDelegate{
func itemsDownloaded(locations:[Location])
}

class HomeModel: NSObject
{
var delegate:HomeModelDelegate?

func chkpwd()
{
//Hit the webservice url

let x = ViewController()
let z = x.loginid

let serviceUrl = "http://www.xyz.in/webservice.php?loginid=(loginid.text)"

//download the json data


let url = URL(string: serviceUrl)

if let url = url {

let session = URLSession(configuration: .default)
let task = session.dataTask(with: url, completionHandler:
{ (data, response, error) in

if error == nil {

//succeeded
self.parseJson(data!)

}
else {
//failed

}
})
task.resume()

}
}

func parseJson(_ data:Data){

var locArray = [Location]()

do{
let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as! [Any]
for jsonResult in jsonArray{

let jsonDict = jsonResult as! [String:String]
let loc = Location(pwd: jsonDict["loginid"]!, loginid: jsonDict["pwd"]!)

locArray.append(loc)

//pass the location back to the delegate

delegate?.itemsDownloaded(locations: locArray)

}
}
catch{
print("An error occured")
}
}
}

最佳答案

请试试这个:

import UIKit

class ViewController: UIViewController,HomeModelDelegate {
var homeModel = HomeModel()

@IBOutlet weak var loginid: UITextField!

@IBOutlet weak var pwd: UITextField!

@IBAction func submit(_ sender: UIButton) {

homeModel.z = loginid.text! // ASSIGNING z here
homeModel.chkpwd()
//Here viewcontroller is assigning itself to the homemodel's delegate property
homeModel.delegate = self

}

override func viewDidLoad() {
super.viewDidLoad()
loginid.layer.cornerRadius=10
pwd.layer.cornerRadius = 10

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func itemsDownloaded(locations: [Location]) {
loginid.text = locations[0].pwd

}
}

型号:

import UIKit

protocol HomeModelDelegate{
func itemsDownloaded(locations:[Location])
}

class HomeModel: NSObject
{
var z:String = "" // INITIALIZING z
var delegate:HomeModelDelegate?

func chkpwd()
{
print(z) // CALLING z
//Hit the webservice url
let serviceUrl = "http://www.xyz.in/webservice.php?loginid=(loginid.text)"

//download the json data

let url = URL(string: serviceUrl)
if let url = url {
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url, completionHandler:
{ (data, response, error) in
if error == nil {
//succeeded
self.parseJson(data!)
} else {
//failed
}
})
task.resume()
}
}

func parseJson(_ data:Data){
var locArray = [Location]()
do{
let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as! [Any]
for jsonResult in jsonArray{
let jsonDict = jsonResult as! [String:String]
let loc = Location(pwd: jsonDict["loginid"]!, loginid: jsonDict["pwd"]!)
locArray.append(loc)

//pass the location back to the delegate
delegate?.itemsDownloaded(locations: locArray)
}
} catch {
print("An error occured")
}
}
}

关于swift - 如何快速将数据从 View Controller 传递到数据模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45901022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com