I am a newbie in swift. I am sure I have missed something really basic. I am trying to initialise a PickerView
in my view, for some reason the list does not show. Nothing happens when I click the picker too. What am I missing ?
Strangely why dont PickerView's
have ID's
like Label
or a TextView
这是ViewController代码
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var pickerView = UIPickerView()
var pickerDataSource = ["White", "Red", "Green", "Blue"];
override func viewDidLoad() {
super.viewDidLoad()
// Initialization for Country Code pickerView
pickerView.dataSource = self
pickerView.delegate = self
pickerView.isHidden = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Methods for Country Code pickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerDataSource.count;
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerDataSource[row]
}
}
修改
我意识到我失去了IBOutlet。要添加它,我按照下面的说明。但是,现在如果我拖动并尝试放入我的Viewcontroller
弹出窗口,则永远不会显示。
另外,我手动添加了这一行,但它仍无效。
@IBOutlet weak var picker: UIPickerView!
编辑2
我试图将OutLet拖放到ViewController。出于某种原因,我现在得到Exception
检查右上方的图片,您会看到
delegate
和'数据源`到视图。这可以吗?
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7faa3a40a900'
*** First throw call stack:
(
0 CoreFoundation 0x000000010dce8d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010d74a21e objc_exception_throw + 48
2 CoreFoundation 0x000000010dd58f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000010dc6e005 ___forwarding___ + 1013
4 CoreFoundation 0x000000010dc6db88 _CF_forwarding_prep_0 + 120
5 UIKit 0x000000010e0ec20f -[UIPickerView _delegateNumberOfComponents] + 56
6 UIKit 0x000000010e0eb33b -[UIPickerView _updateSelectedRows] + 91
7 UIKit 0x000000010e0eb40a -[UIPickerView didMoveToWindow] + 102
8 UIKit 0x000000010e1cb256 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1799
9 UIKit 0x000000010e1cae82 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 819
10 UIKit 0x000000010e1cae82 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 819
11 UIKit 0x000000010e1bdfea __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 151
12 UIKit 0x000000010e1bded8 -[UIView(Hierarchy) _postMovedFromSuperview:] + 857
13 UIKit 0x000000010e1cde56 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1982
14 UIKit 0x000000010e1bc0d7 -[UIView(Hierarchy) addSubview:] + 838
15 UIKit 0x000000010e5d0fdb -[UINavigationTransitionView transition:fromView:toView:] + 645
16 UIKit 0x000000010e2f095b -[UINavigationController _startTransition:fromViewController:toViewController:] + 3314
17 UIKit 0x000000010e2f0ef9 -[UINavigationController _startDeferredTransitionIfNeeded:] + 874
18 UIKit 0x000000010e2f1fdb -[UINavigationController __viewWillLayoutSubviews] + 58
19 UIKit 0x000000010e4e8dd7 -[UILayoutContainerView layoutSubviews] + 223
20 UIKit 0x000000010e1d1ab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
21 QuartzCore 0x000000011368bbf8 -[CALayer layoutSublayers] + 146
22 QuartzCore 0x000000011367f440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
23 QuartzCore 0x000000011367f2be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
24 QuartzCore 0x000000011360d318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
25 QuartzCore 0x000000011363a3ff _ZN2CA11Transaction6commitEv + 475
26 QuartzCore 0x000000011363ad6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
27 CoreFoundation 0x000000010dc8d267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
28 CoreFoundation 0x000000010dc8d1d7 __CFRunLoopDoObservers + 391
29 CoreFoundation 0x000000010dc718a6 CFRunLoopRunSpecific + 454
30 UIKit 0x000000010e106aea -[UIApplication _run] + 434
31 UIKit 0x000000010e10cc68 UIApplicationMain + 159
32 BuzTransit 0x000000010cdb5bbf main + 111
33 libdyld.dylib 0x0000000114ce368d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
编辑2
我现在正确地将我的选择器连接到View Controller
。
但我认为我的pickerView
仍未正确连接。注意下面的图片,我猜这个圆圈应该标有一个胖点。正如我所说,从选择器到ViewController.swift
的拖拽不起作用。
编辑4
我不得不重新启动Xcode并将ViewController名称提到我的StoryBoard。然后我可以拖动这两件事将IBOutlet从我的故事板拖到视图控制器。最后在我的ViewController中连接了FAT DOT的圆圈。
答案 0 :(得分:3)
单击“助理编辑器”按钮,确保故事板位于左侧窗格中,并且ViewController.swift位于右侧。
然后按住控件并单击故事板中的UIPickerView元素并将鼠标拖到右侧。将它放在ViewController类之间并覆盖func viewDidLoad。
将弹出一个小对话框,要求您提供此IBOutlet属性的名称。只需将其命名为“选择器”。
执行此操作后,您的ViewController.swift文件将如下所示:
在ViewController.swift中,添加以下代码。
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var picker: UIPickerView!
var pickerDataSource: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Connect data:
self.picker.delegate = self
self.picker.dataSource = self
// Input data into the Array:
pickerDataSource = ["White", "Red", "Green", "Blue"]
}
然后连接数据源和委托
// Methods for Country Code pickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerDataSource.count;
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerDataSource[row]
}
对于样本,您可以获得tutorial here
更新错误状态
**由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [UIView numberOfComponentsInPickerView:]:无法识别的选择器发送到实例0x7faa3a40a900'
Ans:这意味着你的选择器视图没有正确连接到你当前的类。
连接错误
您已连接到视图
正确之一是您需要与文件所有者联系