ios - Swift PickerView 什么都不显示,没有 Action 没有列表

标签 ios xcode swift3

我是 swift 的新手。我确定我错过了一些非常基本的东西。我正在尝试在我的 View 中初始化一个 PickerView,但出于某种原因,该列表未显示。当我也点击选择器时没有任何反应。我错过了什么?

奇怪的是,为什么 PickerView 的 没有 ID,例如 LabelTextView

enter image description here

这是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

check the image below top right, you will notice delegate and 'datasource` to the View. Is this ok ?

enter image description here

** 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

enter image description here

但我认为我的 pickerView 仍然没有正确连接。注意下图,我猜圆圈应该用一个胖点标记。正如我所说,从选择器到 ViewController.swift 的拖放是行不通的。

编辑 4

我不得不重新启动 Xcode 并在我的 StoryBoard 中提及 ViewController 名称。这两件事我然后能够将 IBOutlet 从我的 Storyboard拖放到 View Controller 。带有 FAT DOT 的圆圈最终连接到我的 ViewController 中。

enter image description here

最佳答案

Click the Assistant Editor button and make sure that the storyboard is in the left pane and that ViewController.swift is in the right.

Then hold down control and click the UIPickerView element in the storyboard and drag your mouse over to the right side. Drop it in between the class ViewController and override func viewDidLoad.

enter image description here

A small dialog will pop up to ask you for a name for this IBOutlet property. Just name it “picker”.

enter image description here

After doing that, your ViewController.swift file will look like this:

enter image description here

In ViewController.swift, add the following code.

   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"]
}

然后连接数据源和委托(delegate)

// 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

更新错误状态

** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7faa3a40a900'

回答:这意味着您的选择器 View 未正确连接到您当前的类(class)。

错误的连接

您已连接到 View

enter image description here

正确的一个是你需要联系你的文件所有者

enter image description here

关于ios - Swift PickerView 什么都不显示,没有 Action 没有列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42672075/

相关文章:

ios - 同步执行多个动画的最佳方式

ios - 找出按钮属于哪个 UITableView

ios - PageViewController 之前和之后调用两次并且索引不计为真

ios - LocalNotification-iOS 7.1模拟器上的UILocalNotificationDefaultSoundName不起作用

ios - tapGestureRecognizer.location 在 UICollectionView 中带来 nil

swift - swift 3.1 中文件管理器的问题

ios - 使用 Swift 4 根据当前字体粗细更改应用程序中的所有 UILabel 字体

ios - UITableview 的动态高度

ios - 点击 iOS 7 时更改背景图像并为 UIButton 设置动画

xcode - 仅适用于 macOS Sierra : library not found for -lAFNetworking