gpt4 book ai didi

ios - Swift-将文本字段从 swift 文件传递​​到其他 swift 文件

转载 作者:行者123 更新时间:2023-11-30 11:23:50 24 4
gpt4 key购买 nike

我是新手我尝试解释我的问题是什么:

我有一个 swift 文件,其名称是 feedmodel.swift:

import Foundation



protocol FeedmodelProtocol: class {
func itemsDownloaded(items: NSArray)
}


class Feedmodel: NSObject, URLSessionDataDelegate {



weak var delegate: FeedmodelProtocol!



func downloadItems() {

let myUrl = URL(string: "http://example.net/stock_service4.php");
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
var request = URLRequest(url:myUrl!)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "Latitudee=19.4&Longitudee=-99.1";
request.httpBody = postString.data(using: .utf8)
let task = defaultSession.dataTask(with: request) { data, response, error in


guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return

}


if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")

}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
self.parseJSON(data)

}

task.resume()

}

我有一个 swift 文件,其名称为 NeuerBeitragViewController:

import UIKit
import CoreLocation


class NeuerBeitragViewController: UIViewController,CLLocationManagerDelegate {



@IBOutlet weak var Tankstelle: UITextField!
@IBOutlet weak var Kraftstoff1: UITextField!
@IBOutlet weak var Preis1: UITextField!
@IBOutlet weak var Kraftstoff2: UITextField!
@IBOutlet weak var Preis2: UITextField!
@IBOutlet weak var Notiz: UITextField!
@IBOutlet weak var Longitude: UITextField!
@IBOutlet weak var Latitude: UITextField!


var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocation!

override func viewDidLoad() {
super.viewDidLoad()




locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
startLocation = nil
}


@IBAction func startWhenInUse(_ sender: Any) {

locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}


func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {



let latestLocation: CLLocation = locations[locations.count - 1]

Latitude.text = String(format: "%.4f",
latestLocation.coordinate.latitude)
Longitude.text = String(format: "%.4f",
latestLocation.coordinate.longitude)

}

func locationManager(_ manager: CLLocationManager,
didFailWithError error: Error) {
print(error.localizedDescription)
}


In my NeuerBeitragViewController.Swift
I have this line:
Latitude.text = String(format: "%.4f",
latestLocation.coordinate.latitude)
Longitude.text = String(format: "%.4f",
latestLocation.coordinate.longitude)

我想获取 Latitude.text 和 Longitude.text 的值在我的 Feedmodel.swift 中的这一行:

 let postString = "firstName=19.4&lastName=-99.1";

这样我就可以在这里执行此操作:

let postString = "firstName=\(Latitude.text)&lastName=\Longitude.text";

希望你们了解我的需要并能提供帮助。

谢谢!

最佳答案

要将信息从一种 View 传递到另一种 View ,您有多种选择:

  • 通过 segue 传递(一对一)
  • 使用协议(protocol)和委托(delegate)(一对一)
  • 使用事件和观察者(一对多)
  • 使用第三个类负责保存当前数据(一对多)

就您而言,使用协议(protocol)代表是正确的选择。

提供示例here .

关于ios - Swift-将文本字段从 swift 文件传递​​到其他 swift 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50974129/

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