我是一個快捷方式的新手。我相信我錯過了一些非常基本的東西。我試圖在我看來初始化一個PickerView
,由於某種原因列表不顯示。當我點擊選取器時也沒有任何反應。我錯過了什麼?Swift PickerView不顯示任何內容,沒有任何操作沒有列表
奇怪的是,爲什麼不PickerView's
有ID's
像Label
或TextView
這裏是視圖控制器代碼
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!
EDIT 2
我試圖拖出口下降到ViewController。出於某種原因,我現在越來越Exception
檢查以下右上方圖片,你會發現
delegate
和「datasource`到視圖。這個可以嗎 ?
** 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)
EDIT 2
我已連接我的選擇器的View Controller
正確了。
但我覺得我的pickerView
仍然沒有正確連接。注意下面的圖片,我猜測圓圈應該用一個胖點標記。正如我所說的從選取器拖到ViewController.swift
只是不起作用。
EDIT 4
我不得不重新啓動Xcode和提及的ViewController的名字我的故事板。這兩件事我可以拖放一個IBOutlet從我的故事板到視圖控制器。最後,我的ViewController中連接了一個FAT DOT的圓圈。
爲UIPickerView在故事板創建IBOutlet中。 –
你是以編程方式創建pickerview的 –
@ Anbu.Karthik不,我沒有以編程方式創建PickerView,我將它拖放到故事板上。 – Siddharth