如何在选择uipickerview或提交页面时从文本描述中获取代码

时间:2014-12-18 11:38:14

标签: ios swift uipickerview

我从UIPickerview(Json回复)填写了国家webservice。 在提交页面或选择说明时,我希望将国家/地区代码存储在某个变量中。

我的代码在

之下
import Foundation
import UIKit
import QuartzCore

class viewAdvanceSearchController: UIViewController,UIPickerViewDataSource, UIPickerViewDelegate
{


@IBOutlet var txtFieldConName: UITextField!
@IBOutlet var txtFieldCompName: UITextField!
@IBOutlet var txtFieldSelectedTown: UITextField!
@IBOutlet var txtFieldSelectedCountry: UITextField!

@IBOutlet var viewImgContact: UIImageView!
@IBOutlet var viewImageCompany: UIImageView!

@IBOutlet var uiPickCountry: UIPickerView!
@IBOutlet var uiPickTown: UIPickerView!

var countryList: [String] = []
var townList: [String] = ["London", "Abingdon"]

@IBOutlet var uiSegmCntrlFavRecent: UISegmentedControl!


override func viewDidLoad() {
    super.viewDidLoad()


    uiPickCountry.tag = 0
    uiPickTown.tag = 1

   /* Fetch country list logic starts */

   // self.view.removeFromSuperview();


    uiPickCountry = UIPickerView(frame: CGRectMake(0, 400, 310, 35))
    uiPickCountry.dataSource = self
    uiPickCountry.delegate = self
    uiPickCountry.showsSelectionIndicator=true
    uiPickCountry.center = view.center
    //uiPickCountry.hidden=true

    //view.addSubview(uiPickCountry)
    txtFieldSelectedCountry.inputView = uiPickCountry


    var resultPage=false
    let url = NSURL(string: "http://URL/getAllCountriesJSON")
    var xmlParse:NSString  = ""
    //var data : NSData!

    let request = NSMutableURLRequest(URL: url!)
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.HTTPMethod = "GET"

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        println("Task completed")
        if(error != nil) {
            // If there is an error in the web request, print it to the console
            println(error.localizedDescription)
        }
        var err: NSError?

        var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
        if(err != nil) {
            // If there is an error parsing JSON, print it to the console
            println("JSON Error \(err!.localizedDescription)")
        }
        if let results = jsonResult["country"] as? NSArray
        {
            for country in results {
                let couCode: String? = country["couCode"] as? String
                let couDescription: String?  = country["couDescription"] as? String
                println(couCode,couDescription)

                self.countryList.append(couDescription!)

            }
        }
         self.txtFieldSelectedCountry.text = self.countryList[0]


    })

    task.resume()







    // Do any additional setup after loading the view, typically from a nib.
}


func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

    return 1

}

func pickerView(pickerView: UIPickerView,numberOfRowsInComponent component: Int) -> Int
{

        return countryList.count

    return 1
}

func pickerView(pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String!{


        return countryList[row]
            return ""

}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

      txtFieldSelectedCountry.text = countryList[row]


}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {

    uiPickCountry.hidden = false


    return false
}
func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{

    self.view.endEditing(true);
    return true;
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }
}

提前致谢。

0 个答案:

没有答案