【问题标题】:Swift- Pass textfield from swift file to other swift fileSwift-将文本字段从 swift 文件传递​​到其他 swift 文件
【发布时间】:2018-12-01 02:47:46
【问题描述】:

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

我有一个名为 feedmodel.swift 的 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()

    }

我有一个名为 NeuerBeitragViewController 的 swift 文件:

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";

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

谢谢!

【问题讨论】:

  • 调用时必须将两个文本字段的text 属性传递给downloadItems。无论如何,您实际上在哪里称呼它?
  • 我不知道你是什么意思

标签: ios swift request pass-data


【解决方案1】:

要将您的信息从一个视图传递到另一个视图,您有多种选择:

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

在您的情况下,使用 ProtocolsDeleguates 是正确的选择。

提供的示例here

【讨论】:

  • 非常感谢你能举个例子吗我是一个完全新手
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多