多个 UIPickerview 错误
Posted
技术标签:
【中文标题】多个 UIPickerview 错误【英文标题】:Multiple UIPickerview Errors 【发布时间】:2015-11-28 00:00:58 【问题描述】:我只是想让你知道我正在使用的 Xcode 版本是 7.1.1。言归正传,我有三个 Pickerview,如下图所示。主pickerview、左pickerview、右pickerview。
主pickerview的数组有一组名字,每个名字有一组单元,左右picker应该有相同的数据。我制作了一本字典,以便将这些单位组同步到它所属的位置,但我做不到。
我将主 Picker 标记为 1,将左侧 Picker 标记为 2,将右侧选取器标记为 3。此外,我已将所有选取器与“dataSource”和“delegate”相关联。
http://s12.postimg.org/fj72kg29p/main_View.png
主要目标是:
我希望当我从主选择器中选择一个名称时,即“区域”,它的组必须显示或出现在左右选择器中等等。
我已经完成了我所做的,但出现以下错误:
http://s1.postimg.org/523f3smwf/Screen_Shot_2015_11_27_at_12_39_20_AM.png
这是我的代码:
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
【问题讨论】:
我已经弄清楚了,在您的 viewDidLoad 中,为什么您已经声明了另一个变量 let dataDict =... 当您已经在类级别(即在 viewDidLoad 之前)声明它时。删除“让”字。 【参考方案1】:同样在您的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;
【讨论】:
谢谢谢谢谢谢你是 SWIFT 中的佼佼者;)谢谢你还不够好。 我只是想问你。现在我想将文本字段链接到左右选择器视图。即:如果从左选择器中选择一米,从右选择器中选择脚,并且我输入 1 米,则脚必须等于 = 0.3048 我在哪里可以执行此公式??? Handle the case in func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) ,意思是跟踪当前在左右选择器中选择的单元,你可能会在一些甚至取决于用户在选择器上所做的选择。这只是一个想法,根据您的需要进行更改。 我能看到一个例子吗?例如:根据我的变量(代码)在 METER 与 FOOT 之间。然后我会做剩下的。以上是关于多个 UIPickerview 错误的主要内容,如果未能解决你的问题,请参考以下文章