多个UIPickerView在同一个UIView中 [英] Multiple UIPickerView in the same UIView

查看:92
本文介绍了多个UIPickerView在同一个UIView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发人员的完全初学者,我想创建一个小iOS应用程序。在这个应用程序中,3 UIPickerViews 应该显示不同的数据。

I'm a complete beginner on iOS dev and I want to create a little iOS application. On this application, 3 UIPickerViews are supposed to display different data.

我的问题在显示屏上。我习惯在Android或Windows手机上开发,我不明白如何使用不同的数据填充 UIPickerViews

My problem is on the display. I'm used to dev on Android or Windows phone and I don't understand how to populate the UIPickerViews with different data.

这是我已编写的代码:

//
//  ViewController.swift
//  iphoneVersion
//
//  Created by fselva on 13/05/2015.
//  Copyright (c) 2015 fselva. All rights reserved.
//

import UIKit



class ViewController: UIViewController, UIPickerViewDelegate{



@IBOutlet weak var pickerView1: UIPickerView!
@IBOutlet weak var pickerView2: UIPickerView!
@IBOutlet weak var pickerView3: UIPickerView!

var test = ["Todo","Waiting","Maybe","Inbox","Note"]

var test2 = ["@Office","@Computer","@Home","@Meeting", "@Read", "@Achat", "@Call"]



override func viewDidLoad() {
    super.viewDidLoad()
    pickerView1.tag = 1
    pickerView2.tag = 2
    pickerView3.tag = 3
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfComponentsInPickerView(pickerView : UIPickerView!) -> Int{
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
    if pickerView2.tag == 2 {
        return test.count
    } else if pickerView3 == 3{
        return test2.count
    }
    return 1
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

    if pickerView2 == 2 {
        return test[row]
    } else if pickerView3 == 3{
        return test2[row]
    }
        return ""
}


}

@IBOutlet weak var pickerView1:UIPickerView!已经通过Ctrl + Click从故事板自动创建到ViewController.swift。

@IBOutlet weak var pickerView1: UIPickerView!has been automatically created by Ctrl+Click from the storyboard to the ViewController.swift.

第一个 UIPickerView 目前应该什么都不显示。第二个应该显示 test ,第三个显示 test2

The first UIPickerView is currently supposed to display nothing. The second one is supposed to display test and the third one displays test2.

在互联网上经过数小时的调查后,我听说过标签来定义谁必须展示什么,但它不起作用。

After hours of investigation on internet, I have heard about tags to define who must display what, but it doesn't work.

我做错了什么,我错过了什么地方吗?

Am I doing something wrong, am I missing something somewhere?

推荐答案

你不需要标签,只需使用:

You don't need tags, just use:

 func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        if pickerView == pickerView1 {
            //pickerView1
        } else if pickerView == pickerView2{
           //pickerView2
        }

也不要忘记在IB或代码中设置委托:

also don't forget to set delegate in IB or in code:

pickerView1.delegate = self

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

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