iOS开发技巧 - 使用UIPickerView来选择值
(Swift)
import UIKit
class 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
}
return
}
// returns the number of rows in each component
func pickerView(pickerView: UIPickerView,
numberOfRowsInComponent component: Int) -> Int {
if pickerView == picker {
return
}
return
}
}
func pickerView(pickerView: UIPickerView,
titleForRow row: Int,
forComponent component: Int) -> String! {
return "\(row + 1)"
}
(Objective-C)
@interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
@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 ;
}
return ;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
if ([pickerView isEqual:self.myPicker]){
return ;
}
return ;
}
- (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 + ];
}
return nil;
}
iOS开发技巧 - 使用UIPickerView来选择值的更多相关文章
- iOS开发技巧 - 使用UISlider来调整值的范围
(Swift) import UIKit class ViewController: UIViewController { var slider: UISlider! func sliderValue ...
- iOS开发技巧 - 使用UIDatePicker来选择日期和时间
(Swift) import UIKit class ViewController: UIViewController { var datePicker: UIDatePicker! func dat ...
- iOS开发技巧系列---详解KVC(我告诉你KVC的一切)
KVC(Key-value coding)键值编码,单看这个名字可能不太好理解.其实翻译一下就很简单了,就是指iOS的开发中,可以允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值.而不需 ...
- 【转】几点 iOS 开发技巧
[译] 几点 iOS 开发技巧 原文:iOS Programming Architecture and Design Guidelines 原文来自破船的分享 原文作者是开发界中知晓度相当高的 Mug ...
- iOS开发技巧
一.寻找最近公共View 我们将一个路径中的所有点先放进 NSSet 中.因为 NSSet 的内部实现是一个 hash 表,所以查找元素的时间复杂度变成了 O(1),我们一共有 N 个节点,所以总时间 ...
- iOS开发技巧系列---使用链式编程和Block来实现UIAlertView
UIAlertView是iOS开发过程中最常用的控件之一,是提醒用户做出选择最主要的工具.在iOS8及后来的系统中,苹果更推荐使用UIAlertController来代替UIAlertView.所以本 ...
- iOS开发技巧 -- 复用代码片段
如果你是一位开发人员在开发过程中会发现有些代码无论是在同一个工程中还是在不同工程中使用率会很高,有经验的人会直接封装在一个类里,或者写成一个宏定义或者把这些代码收集起来,下次直接使用,或者放到xcod ...
- iOS开发技巧 - Size Class与iOS 8多屏幕适配(一)
0. 背景: 在iOS开发中,Auto Layout(自动布局)能解决大部分的屏幕适配问题. 但是当iPhone 6和iPhone 6 Plus发布以后, Auto Layout已经不能解决复杂的屏幕 ...
- 几点iOS开发技巧
转自I'm Allen的博客 原文:iOS Programming Architecture and Design Guidelines 原文来自破船的分享 原文作者是开发界中知晓度相当高 ...
随机推荐
- 总结ASP.NET MVC视图页使用jQuery传递异步数据的几种方式
在ASP.NET MVC的视图页向控制器传递异步数据,可能是数组,JavaScript对象,json,表单数据,等等. 关于数据,JavaScript对象有时候和json长得一模一样,有么有? var ...
- (转)Java中的守护线程
Java的守护线程与非守护线程 守护线程与非守护线程 最近在看多线程的Timer章节,发现运用到了守护线程,感觉Java的基础知识还是需要补充. Java分为两种线程:用户线程和守护线程 所谓守护 ...
- ibatis.net:第二天,Hello,World ?
背景 本文的内容全部来自于官方的文档,此处仅仅为了强化记忆. 项目结构 Properties.config <?xml version="1.0" encoding=&quo ...
- Java 如何实现在线预览文档及修改(文本文件)
暂时未解决的问题:多用户并发修改一个文件 测试地址: http://sms.reyo.cn 用户名:aa 密码:123456
- ubuntu声音问题
在终端查看声卡: aplay -l 成功显示: **** PLAYBACK硬件设备列表 **** card 0: Intel [HDA Intel], device 0: ALC662 rev1 An ...
- 对Java通配符的个人理解(以集合为例)
对Java通配符的个人理解(以集合为例) 前言:最近在学习Java,当学到了泛型的通配符时,不是很理解PECS(Producer Extends Consumer Super)原则,以及<? e ...
- crc16.c
static unsigned char auchCRCHi[];static unsigned char auchCRCLo[]; /* CRC 高位字节值表 */static unsigned c ...
- PreApplicationStartMethodAttribute程序启动扩展
一.PreApplicationStartMethodAttribute 类简介 应用程序启动时提供的扩展的支持. 命名空间: System.Web程序集: System.Web(位于 Syst ...
- 在LaTeX中使用颜色 Using colours in LaTeX
Using colours in LaTeX There are several elements in LATEX whose colour can be changed to improve th ...
- ASP.NET MVC 中使用JavaScriptResult asp.net mvc 返回 JavaScript asp.mvc 后台返回js
return this.Content("<script>alert('暂无!');window.location.href='/Wap/Index';</script&g ...