博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发技巧 - 使用UIPickerView来选择值
阅读量:6614 次
发布时间:2019-06-24

本文共 2483 字,大约阅读时间需要 8 分钟。

(Swift)

import UIKitclass ViewController: UIViewController, UIPickerViewDataSource {    var picker: UIPickerView!        override func viewDidLoad() {        super.viewDidLoad()                picker = UIPickerView()                // select the current view controller as the data source of the picker view        picker.dataSource = self        picker!.delegate = self                picker.center = view.center        view.addSubview(picker)    }        /*        Implemented some of the methods of the UIPickerViewDataSource protocol    */        // returns the number of 'columns' to display    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {        if pickerView == picker {            return 1        }        return 0    }        // returns the number of rows in each component    func pickerView(pickerView: UIPickerView,        numberOfRowsInComponent component: Int) -> Int {        if pickerView == picker {            return 10        }        return 0    }}func pickerView(pickerView: UIPickerView,    titleForRow row: Int,    forComponent component: Int) -> String! {    return "\(row + 1)"}

 

(Objective-C)

@interface ViewController () 
@property (nonatomic, strong) UIPickerView *myPicker;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.myPicker = [[UIPickerView alloc] init]; // select the current view controller as the data source of the picker view self.myPicker.dataSource = self; self.myPicker.delegate = self; self.myPicker.center = self.view.center; [self.view addSubview:self.myPicker];}/* Implemented some of the methods of the UIPickerViewDataSource protocol*/- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { if ([pickerView isEqual:self.myPicker]){ return 1; } return 0;}- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if ([pickerView isEqual:self.myPicker]){ return 10; } return 0;}- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if ([pickerView isEqual:self.myPicker]) { /* Row is zero-based and we want the first row (with index 0) to be rendered as Row 1, so we have to +1 every row index */ return [NSString stringWithFormat:@"Row %ld", (long)row + 1]; } return nil;}

 

转载于:https://www.cnblogs.com/davidgu/p/5773764.html

你可能感兴趣的文章
Boyer-Moore 算法介绍
查看>>
关于Java中的单例模式
查看>>
datepicker
查看>>
基于vCenter/ESXi平台CentOS 6.8系统虚拟机Oracle 12c RAC双节点数据库集群搭建
查看>>
CentOS 7输入startx无法启动图形化界面
查看>>
#51CTO学院四周年# 终于在这里遇到你
查看>>
百度首次公布云业务收入,同比增长超100%,跻身国内第三
查看>>
Java学习笔记 1—命名规则、数据类型、运算符
查看>>
FusionCharts入门教程,使用指南
查看>>
我的友情链接
查看>>
数组的一些方法
查看>>
关于MFC中WM_MOUSEHOVER和WM_MOUSELEAVE消息的使用
查看>>
我的友情链接
查看>>
linux下查看nginx,apache,mysql,php的编译参数[转]
查看>>
Android掌中游斗地主游戏源码完整版
查看>>
LeetCode - 26. 删除排序数组中的重复项
查看>>
Linux LVM逻辑卷配置过程详解
查看>>
关于IT服务管理的服务台
查看>>
rundeck 修改密码 添加节点
查看>>
IT讲师韩顺平:创业不易,尚硅谷延续教育初心
查看>>