(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来选择值的更多相关文章

  1. iOS开发技巧 - 使用UISlider来调整值的范围

    (Swift) import UIKit class ViewController: UIViewController { var slider: UISlider! func sliderValue ...

  2. iOS开发技巧 - 使用UIDatePicker来选择日期和时间

    (Swift) import UIKit class ViewController: UIViewController { var datePicker: UIDatePicker! func dat ...

  3. iOS开发技巧系列---详解KVC(我告诉你KVC的一切)

    KVC(Key-value coding)键值编码,单看这个名字可能不太好理解.其实翻译一下就很简单了,就是指iOS的开发中,可以允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值.而不需 ...

  4. 【转】几点 iOS 开发技巧

    [译] 几点 iOS 开发技巧 原文:iOS Programming Architecture and Design Guidelines 原文来自破船的分享 原文作者是开发界中知晓度相当高的 Mug ...

  5. iOS开发技巧

    一.寻找最近公共View 我们将一个路径中的所有点先放进 NSSet 中.因为 NSSet 的内部实现是一个 hash 表,所以查找元素的时间复杂度变成了 O(1),我们一共有 N 个节点,所以总时间 ...

  6. iOS开发技巧系列---使用链式编程和Block来实现UIAlertView

    UIAlertView是iOS开发过程中最常用的控件之一,是提醒用户做出选择最主要的工具.在iOS8及后来的系统中,苹果更推荐使用UIAlertController来代替UIAlertView.所以本 ...

  7. iOS开发技巧 -- 复用代码片段

    如果你是一位开发人员在开发过程中会发现有些代码无论是在同一个工程中还是在不同工程中使用率会很高,有经验的人会直接封装在一个类里,或者写成一个宏定义或者把这些代码收集起来,下次直接使用,或者放到xcod ...

  8. iOS开发技巧 - Size Class与iOS 8多屏幕适配(一)

    0. 背景: 在iOS开发中,Auto Layout(自动布局)能解决大部分的屏幕适配问题. 但是当iPhone 6和iPhone 6 Plus发布以后, Auto Layout已经不能解决复杂的屏幕 ...

  9. 几点iOS开发技巧

    转自I'm Allen的博客   原文:iOS Programming Architecture and Design Guidelines   原文来自破船的分享   原文作者是开发界中知晓度相当高 ...

随机推荐

  1. Windows Phone本地数据库(SQLCE):5、[Association]attribute(翻译)(转)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第五篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...

  2. ASIHTTPRequest系列(一):同步和异步请求

    ASIHTTPRequest系列(一):同步和异步请求 发表于8个月前(2013-11-27 19:21)   阅读(431) | 评论(0) 6人收藏此文章, 我要收藏 赞0 ASIHTTPRequ ...

  3. Getting OS version with NDK in C c++获得版本号

    http://stackoverflow.com/questions/19355783/getting-os-version-with-ndk-in-c #include <cutils/pro ...

  4. Java 8新的时间日期库的20个使用示例

    原文链接 作者:Javin Paul 译者:之诸暇 除了lambda表达式,stream以及几个小的改进之外,Java 8还引入了一套全新的时间日期API,在本篇教程中我们将通过几个简单的任务示例来学 ...

  5. 实用ExtJS教程100例-007:ExtJS中Window组件最小化

    在上一节中我们演示了如何使用ExtJS的Window组件,这篇内容中我们来演示一下如何将窗口最小化. 要让ExtJS标题栏中显示最小化按钮并不麻烦,只需要设置 minimizable: true 即可 ...

  6. 用开源项目cropper实现对图片中任意部分进行裁剪

     红色区域为截图控件的区域.    开源项目地址:https://github.com/edmodo/cropper croper这个开源项目可以对一个图片进行任意区域的街区,并且可以设置图片的旋转角 ...

  7. 第一章 AOP

    关于AOP,通常我们会使用AspectJ注解来做,共有6中切面 前置:@Before 后置:@After 返回值:@AfterReturing 异常:@AfterThrowing 环绕:@Around ...

  8. cannot import name 'main' 解决方案

    error description: pip3 install numpy Traceback (most recent call last): File "/usr/bin/pip3&qu ...

  9. IP组播

    1  IP组播基础 IP组播技术有效地解决了单点发送.多点接收的问题.组播源只发送一份数据,被传递的信息在距组播源尽可能远的网络节点才开始被复制和分发,并且只发送给需要该信息的接收者.  说明: 本章 ...

  10. ASP.NET Razor C# 和 VB 代码语法

    ylbtech-.NET: ASP.NET Razor  C# 和 VB 代码语法 Razor 不是一种编程语言.它是服务器端的标记语言. 1. C# 和 VB 代码语法返回顶部 Razor 同时支持 ...