多个UIPickerview错误 [英] Multiple UIPickerview Errors

本文介绍了多个UIPickerview错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想让您知道我正在使用的Xcode版本是7.1.1.到目前为止,我有三个Pickerviews,如下图所示.主pickerview,左pickerview和右pickerview.

主pickerview的数组有一组名称,每个名称都有一组单位,并且左右选择器应该具有相同的数据.我制作了一个字典,以便将这些单元组同步到它所属的位置,但是我做不到.

我将主Picker标记为1,将左侧Picker标记为2,将右侧Picker标记为3.此外,我将所有它们与"dataSource"和& 委托".

http://s12.postimg.org/fj72kg29p/main_View.png


主要目标是:

我想要从主选择器中选择一个名称(即区域")时,其组必须显示或出现在左右选择器中,依此类推.

我已经完成了自己的工作,但是出现了以下错误:

http://s1.postimg.org/523f3smwf/Screen_Shot_2015_11_27_at_12_39_20_AMpng./p>

这是我的代码:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {

    @IBOutlet weak var mainPicker: UIPickerView!
    @IBOutlet weak var leftPicker: UIPickerView!
    @IBOutlet weak var rightPicker: UIPickerView!
    @IBOutlet weak var textFieldLeft: UITextField!
    @IBOutlet weak var textFielfRight: UITextField!
    @IBOutlet weak var equal: UILabel!

    //var mainPickerData = []
    var leftPickerData = []
    var rightPickerData = []

    var dataDict:NSMutableDictionary!
    var mainPickerData:NSArray!
    var leftRightPickerData:NSArray!

    //yourPicker.backgroundColor = UIColor(patternImage: UIImage(named: "back.jpg")!)


    override func viewDidLoad() {
        super.viewDidLoad()



        // Connect data:
        self.mainPicker.delegate = self
        self.mainPicker.dataSource = self

        self.leftPicker.delegate = self
        self.leftPicker.dataSource = self

        self.rightPicker.delegate = self
        self.rightPicker.dataSource = self

        let theWidth = view.frame.size.width
        let theHeight = view.frame.size.height

        mainPicker.center = CGPointMake(theWidth/2, theHeight/2 - 182.5)
        leftPicker.center = CGPointMake(theWidth/2 - 100, theHeight/2)
        rightPicker.center = CGPointMake(theWidth/2 + 100, theHeight/2)
        textFieldLeft.center = CGPointMake(theWidth/2 - 90, theHeight/2 + 110)
        textFielfRight.center = CGPointMake(theWidth/2 + 90, theHeight/2 + 110)
        equal.center = CGPointMake(theWidth/2, theHeight/2 + 110)



        let dataDict:NSMutableDictionary = ["Area":["Square Mile", "Square Yard", "Square Foot", "Square Inch", "Hectare", "Acre", "Square Kilometer", "Square Meter", "Square Centimeter", " Square Millimeter"]
            ,"Energy":["Btus", "Calories", "Ergs", "Foot-Pounds", "Joules", "Kilogram-Calories", "Kilogram-Meters", "Kilowatt-Hours", "Newton-Meters", "Watt-Hours"], "Length":["Mile", "Yard", "Foot", "Inch", "Kilometer", "Meter", "Centimeter", "Millimeter"], "Power": ["Btus/Minute", "Foot-Pounds/Min", "Foot-Pounds/Sec", "Horsepower", "Kilowatts", "Watts"], "Pressure": ["Pounds/Sqr Ft", "Pounds/Sqr In", "Atmospheres", "Bars", "In of Mercury", "Cm of Mercury", "Kilograms/Sqr Meter", "Pascals"], "Speed": ["Knots", "Miles/Hr", "Miles/Min", "Feet/Min", "Feet/Sec", "Kilometers/Hr", "Kilometer/Min", "Meters/Sec"], "Temperature": ["Celsius C˚", "Fahrenheit", "Kelvin"], "Time": ["Years", "Months", "Weeks", "Days", "Hours", "Minutes", "Seconds", "Millisconds", "Microseconds", " Nanoseconds"], "Volume": ["Cupic Feet","Cubic Meter", "Gallon (Imp)", "Gallon (US)", "Quart (US)", "Pint (US)", "Fluid Oz", "Cup", "Tablespoon", "Teaspoon", "Dram (US)", "Liter"], "Weight": ["Short Ton (US)","Long Ton (UK)", "Pound (U.S)", "Ounce (US)", "Stone", "Metric Ton", "Kilogram", "Gram"]]
        mainPickerData = dataDict.allKeys;
        leftRightPickerData = dataDict.objectForKey(mainPickerData.firstObject as! String) as! NSArray

        // Linking the textFields with the pickerViews.
        textFieldLeft.inputView = leftPicker;
        textFielfRight.inputView = rightPicker;


        }

    // The number of columns of data
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    // The number of rows of data
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        switch (pickerView.tag) {

        case mainPicker.tag:

            return mainPickerData.count

        case leftPicker.tag,rightPicker.tag:

            let currentSelectedIndex = mainPicker.selectedRowInComponent(0)
            leftRightPickerData = (dataDict.objectForKey(mainPickerData[currentSelectedIndex] as! String) as! NSArray)

            return leftRightPickerData.count;

        default:
            break;
        }
        return 0;
    }

    // The data to return for the row and component (column) that's being passed in
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        if leftPicker.tag == 1 {

            return leftPickerData[row] as? String

        }else {

            return rightPickerData[row] as? String

        }


    }

    // Catpure the picker view selection
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        // This method is triggered whenever the user makes a change to the picker selection.
        // The parameter named row and component represents what was selected.

        if(pickerView.tag == 1 ){

            let currentSelectedIndex = mainPicker.selectedRowInComponent(0)
            leftRightPickerData = (dataDict.objectForKey(mainPickerData[currentSelectedIndex] as! String) as! NSArray)

            leftPicker.reloadAllComponents();
            rightPicker.reloadAllComponents();
        }

    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        self.view.endEditing(true)

    }

    func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let titleData = mainPickerData[row]
        let myTitle = NSAttributedString(string: titleData as! String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.blueColor()])
        return myTitle
    }



}

在您的func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString?中,还可以正确处理所有选择器的属性文本,否则您的代码将崩溃.您只需为mainPicker定义attributedTitle.对其他人也一样,如下所示.根据您的需要进行更改.

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    let titleData : String?
    if(pickerView.tag == mainPicker.tag){
        titleData = mainPickerData[row] as? String;

    }
    else{

        let currentSelectedIndex = mainPicker.selectedRowInComponent(0)
        leftRightPickerData = (dataDict.objectForKey(mainPickerData[currentSelectedIndex] as! String) as! NSArray)

        titleData = leftRightPickerData[row] as? String;
    }

    let myTitle = NSAttributedString(string: titleData!, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.blueColor()])
    return myTitle;

}

I just wanna let you know that the version of Xcode that I'm working on is 7.1.1. Getting to the point, I have three Pickerviews as is shown in the image below. Main pickerview, left pickerview, and right pickerview.

The array of main pickerview has a group of names, and each name has a group of units, and the right and left pickers are supposed to have the same data. I have made a dictionary in order to sync those groups of units to where it belongs, but I could not do it.

I tagged the main Picker with 1, the left Picker with 2, and the right picker with 3. Also, all the pickers I have linked them with "dataSource" & "delegate".

http://s12.postimg.org/fj72kg29p/main_View.png


The main goal is:

I want when I pick a name i.e "Area" from the main picker its group must show or appear in the left and right pickers and so on.

I have done what I have done but I got the following error:

http://s1.postimg.org/523f3smwf/Screen_Shot_2015_11_27_at_12_39_20_AM.png

Here is my code:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {

    @IBOutlet weak var mainPicker: UIPickerView!
    @IBOutlet weak var leftPicker: UIPickerView!
    @IBOutlet weak var rightPicker: UIPickerView!
    @IBOutlet weak var textFieldLeft: UITextField!
    @IBOutlet weak var textFielfRight: UITextField!
    @IBOutlet weak var equal: UILabel!

    //var mainPickerData = []
    var leftPickerData = []
    var rightPickerData = []

    var dataDict:NSMutableDictionary!
    var mainPickerData:NSArray!
    var leftRightPickerData:NSArray!

    //yourPicker.backgroundColor = UIColor(patternImage: UIImage(named: "back.jpg")!)


    override func viewDidLoad() {
        super.viewDidLoad()



        // Connect data:
        self.mainPicker.delegate = self
        self.mainPicker.dataSource = self

        self.leftPicker.delegate = self
        self.leftPicker.dataSource = self

        self.rightPicker.delegate = self
        self.rightPicker.dataSource = self

        let theWidth = view.frame.size.width
        let theHeight = view.frame.size.height

        mainPicker.center = CGPointMake(theWidth/2, theHeight/2 - 182.5)
        leftPicker.center = CGPointMake(theWidth/2 - 100, theHeight/2)
        rightPicker.center = CGPointMake(theWidth/2 + 100, theHeight/2)
        textFieldLeft.center = CGPointMake(theWidth/2 - 90, theHeight/2 + 110)
        textFielfRight.center = CGPointMake(theWidth/2 + 90, theHeight/2 + 110)
        equal.center = CGPointMake(theWidth/2, theHeight/2 + 110)



        let dataDict:NSMutableDictionary = ["Area":["Square Mile", "Square Yard", "Square Foot", "Square Inch", "Hectare", "Acre", "Square Kilometer", "Square Meter", "Square Centimeter", " Square Millimeter"]
            ,"Energy":["Btus", "Calories", "Ergs", "Foot-Pounds", "Joules", "Kilogram-Calories", "Kilogram-Meters", "Kilowatt-Hours", "Newton-Meters", "Watt-Hours"], "Length":["Mile", "Yard", "Foot", "Inch", "Kilometer", "Meter", "Centimeter", "Millimeter"], "Power": ["Btus/Minute", "Foot-Pounds/Min", "Foot-Pounds/Sec", "Horsepower", "Kilowatts", "Watts"], "Pressure": ["Pounds/Sqr Ft", "Pounds/Sqr In", "Atmospheres", "Bars", "In of Mercury", "Cm of Mercury", "Kilograms/Sqr Meter", "Pascals"], "Speed": ["Knots", "Miles/Hr", "Miles/Min", "Feet/Min", "Feet/Sec", "Kilometers/Hr", "Kilometer/Min", "Meters/Sec"], "Temperature": ["Celsius C˚", "Fahrenheit", "Kelvin"], "Time": ["Years", "Months", "Weeks", "Days", "Hours", "Minutes", "Seconds", "Millisconds", "Microseconds", " Nanoseconds"], "Volume": ["Cupic Feet","Cubic Meter", "Gallon (Imp)", "Gallon (US)", "Quart (US)", "Pint (US)", "Fluid Oz", "Cup", "Tablespoon", "Teaspoon", "Dram (US)", "Liter"], "Weight": ["Short Ton (US)","Long Ton (UK)", "Pound (U.S)", "Ounce (US)", "Stone", "Metric Ton", "Kilogram", "Gram"]]
        mainPickerData = dataDict.allKeys;
        leftRightPickerData = dataDict.objectForKey(mainPickerData.firstObject as! String) as! NSArray

        // Linking the textFields with the pickerViews.
        textFieldLeft.inputView = leftPicker;
        textFielfRight.inputView = rightPicker;


        }

    // The number of columns of data
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    // The number of rows of data
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        switch (pickerView.tag) {

        case mainPicker.tag:

            return mainPickerData.count

        case leftPicker.tag,rightPicker.tag:

            let currentSelectedIndex = mainPicker.selectedRowInComponent(0)
            leftRightPickerData = (dataDict.objectForKey(mainPickerData[currentSelectedIndex] as! String) as! NSArray)

            return leftRightPickerData.count;

        default:
            break;
        }
        return 0;
    }

    // The data to return for the row and component (column) that's being passed in
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        if leftPicker.tag == 1 {

            return leftPickerData[row] as? String

        }else {

            return rightPickerData[row] as? String

        }


    }

    // Catpure the picker view selection
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        // This method is triggered whenever the user makes a change to the picker selection.
        // The parameter named row and component represents what was selected.

        if(pickerView.tag == 1 ){

            let currentSelectedIndex = mainPicker.selectedRowInComponent(0)
            leftRightPickerData = (dataDict.objectForKey(mainPickerData[currentSelectedIndex] as! String) as! NSArray)

            leftPicker.reloadAllComponents();
            rightPicker.reloadAllComponents();
        }

    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        self.view.endEditing(true)

    }

    func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let titleData = mainPickerData[row]
        let myTitle = NSAttributedString(string: titleData as! String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.blueColor()])
        return myTitle
    }



}

解决方案

Also in your func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? properly handle attributed text for all the pickers otherswise your code will crash. You just have define attributedTitle for mainPicker. Do it for others too like the following.Make changes as per your need.

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    let titleData : String?
    if(pickerView.tag == mainPicker.tag){
        titleData = mainPickerData[row] as? String;

    }
    else{

        let currentSelectedIndex = mainPicker.selectedRowInComponent(0)
        leftRightPickerData = (dataDict.objectForKey(mainPickerData[currentSelectedIndex] as! String) as! NSArray)

        titleData = leftRightPickerData[row] as? String;
    }

    let myTitle = NSAttributedString(string: titleData!, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.blueColor()])
    return myTitle;

}

这篇关于多个UIPickerview错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆