2017-08-23 3 views
0

私はこの機能をSwiftファイルに入れ、データベースのデータの値を返して弁護士に出力します。 他のView Controllerで値を使用したいが、これを動作させることができないので、誰かが私を助けてくれることを願っています。 nameUserstatusUserpointUser他のView Controllerで使うのが好きです。別のView ControllerでXcode funcが使用されました

import Foundation 
    import UIKit 
    var code = "100" 
    var getStatusUSer = "" 

class getJSON: NSObject, URLSessionDataDelegate 
{ 
//properties 
var data : NSMutableData = NSMutableData() 

func downloadItems() 
{ 
let url = NSMutableURLRequest(url: NSURL(string:       "http://www.hholm.dk/time_app/qrcode4.php")! as URL) 
    url.httpMethod = "POST" 
    let postString = "username=\(code)" 
    url.httpBody = postString.data(using: String.Encoding.utf8) 
    print(url.httpBody = postString.data(using: String.Encoding.utf8)) 

    var session: URLSession! 
    let configuration = URLSessionConfiguration.default 

    session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil) 

    let task = session.dataTask(with: url as URLRequest) 

    task.resume() 

} 

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) 
{ 
    self.data.append(data as Data); 
} 

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) 
{ 
    if error != nil 
    { 
     print("Not Found", error) 

    } 
    else 
    { 
     print("Ok") 
     self.parseJSON() 
    } 

} 


func parseJSON() 
{ 

    var jsonResult: NSArray = NSArray() 

    do 
    { 
     jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray 

     print("jsonResult.count",jsonResult.count) 
    } 
    catch let error as NSError 
    { 
     print("jsonResult: ", error) 
    } 


    var jsonElement: NSDictionary = NSDictionary() 
    var contador = 0 
    for i in (0..<jsonResult.count) 
    { 
     jsonElement = jsonResult[i] as! NSDictionary 

     if let nameUser = jsonElement["name"] as? String, 
      let pointUser = jsonElement["point"] as? String, 
      let statusUser = jsonElement["status"] as? String 
     { 
      getStatusUSer = statusUser 
      print("Name: ", nameUser) 
      print("Status: ", statusUser) 
      print("Point: ", pointUser) 

     } 

    } 
} 

} 

こんにちはウーフこれは私が私のviewcontrolerに持っているものです。

import UIKit 

class inputcodeViewController: UIViewController { 

@IBOutlet weak var input: UITextField! 

@IBAction func but(_ sender: Any) { 
    downloadItems() 
      } 


func downloadItems(){ 
    let getJson = GetJSON() 
    //setting the delegate 
    getJson.delegate = self 
    //starting download 
    getJson.downloadItems() 
} 
} 

    extension inputcodeViewController: GetJSONDelegate { 
    func didReceiveValues(name: String, status: String, point: String){ 
     //now you can use values in your view controller 
    } 

} 

は、どのように私はあなたがこれらの値を返すために、プロトコルを使用することができます値

+0

ビューコントローラから関数を取り出し、それをクラスに入れ、そのクラスを使用します。 View Controllerが他のView Controllerで関数を呼び出すのは良い方法ではありません。 – ryantxr

+0

こんにちはryantxr、eksempelがありますか?私は、swiftfileでfuncを持っていますが、viewcontrolerファイラーではありません。申し訳ありませんが、私はxcodeに新しいです。 –

答えて

0

を印刷することができます。

import Foundation 
import UIKit 
var code = "100" 
var getStatusUSer = "" 

//define the protocol 
protocol GetJSONDelegate { 
     func didReceiveValues(name: String, status: String, point: String) 
} 
//I've changed the first char of the class name to uppercase 
class GetJSON: NSObject, URLSessionDataDelegate{ 
//properties 
var data : NSMutableData = NSMutableData() 

//delegate 
var delegate: GetJSONDelegate? 

func downloadItems(){ 
let url = NSMutableURLRequest(url: NSURL(string:       "http://www.hholm.dk/time_app/qrcode4.php")! as URL) 
    url.httpMethod = "POST" 
    let postString = "username=\(code)" 
    url.httpBody = postString.data(using: String.Encoding.utf8) 
    print(url.httpBody = postString.data(using: String.Encoding.utf8)) 

    var session: URLSession! 
    let configuration = URLSessionConfiguration.default 

    session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil) 

    let task = session.dataTask(with: url as URLRequest) 

    task.resume() 

} 

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) 
{ 
    self.data.append(data as Data); 
} 

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) 
{ 
    if error != nil 
    { 
     print("Not Found", error) 

    } 
    else 
    { 
     print("Ok") 
     self.parseJSON() 
    } 

} 


func parseJSON() 
{ 

    var jsonResult: NSArray = NSArray() 

    do 
    { 
     jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray 

     print("jsonResult.count",jsonResult.count) 
    } 
    catch let error as NSError 
    { 
     print("jsonResult: ", error) 
    } 


    var jsonElement: NSDictionary = NSDictionary() 
    var contador = 0 
    for i in (0..<jsonResult.count) 
    { 
     jsonElement = jsonResult[i] as! NSDictionary 

     if let nameUser = jsonElement["name"] as? String, 
      let pointUser = jsonElement["point"] as? String, 
      let statusUser = jsonElement["status"] as? String 
     { 
      getStatusUSer = statusUser 
      print("Name: ", nameUser) 
      print("Status: ", statusUser) 
      print("Point: ", pointUser) 

      //here we will return received data to the delegate 
      self.delegate?.didReceiveValues(name: nameUser, status: statusUser, point: pointUser) 
     } 

    } 
} 

} 

これでコントローラをそのプロトコルの代理人として設定する必要があります:

//this is an example, you need to add the methods described in your controller where you want to use those values 

class YourViewController: UIViewController{ 
    // the method that is called by you to get values 
    func downloadItems(){ 
      let getJson = GetJSON() 
      //setting the delegate 
      getJson.delegate = self 
      //starting download 
      getJson.downloadItems() 
    } 
} 

//defining protocol methods in the extension of the view controller 
extension YourViewController: GetJSONDelegate { 
    func didReceiveValues(name: String, status: String, point: String){ 
      //now you can use values in your view controller 
    } 
} 
+0

ありがとうございました。これはうまくいくようですが、ポイントだけを印刷したい場合は、どうすればviewcontrolerのポイントを印刷できますか。 –

+0

あなたは何をするつもりなのか分かりません。コントローラでこれらのパラメータを使用したいと言ったので、ここでは必要に応じて使用します。 1つだけを送信する場合は、必要に応じてプロトコルfuncを変更します。また、あなたの拡張ビューのYourViewController:GetJSONDelegateとself.delegateで同じ方法で変更する必要がありますか?.didReceiveValues – Woof

+0

こんにちは、ビューアでこのパラメータを印刷する方法を私に示すことができます。ボタン –

関連する問題