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 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 () <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 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;
}
时间: 2024-08-24 14:42:25

iOS开发技巧 - 使用UIPickerView来选择值的相关文章

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

(Swift) import UIKit class ViewController: UIViewController { var datePicker: UIDatePicker! func datePickerDateChanged(datePicker: UIDatePicker) { println("Selected date = \(datePicker.date)") } override func viewDidLoad() { super.viewDidLoad()

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

(Swift) import UIKit class ViewController: UIViewController { var slider: UISlider! func sliderValueChanged(slider: UISlider) { println("Slider's new value is \(slider.value)") } override func viewDidLoad() { super.viewDidLoad() slider = UISlide

iOS开发技巧之查看模拟器沙盒文件

iOS开发技巧之查看模拟器沙盒文件 iOS开发中,在对数据库进行操作时,有时我们需要直观的查看数据库的内容,那么我们如何找到沙盒中的这个文件呢,步骤很简单: 1.点击Finder选项栏上的前往菜单: 2.选择前往文件夹选项: 前往的文件路径为:/Users/username/Library/Application Support/iPhone Simulator/ 其中username为当前mac电脑的用户名. 3.界面类似如下模样,选择一个版本的模拟器,应用的沙盒文件就在Application

iOS开发技巧(系列十七:使用Xcode DEBUG模式和RELEASE模式)

在开发过程中,我们经常需要用到NSLog输出一些信息,甚至有的开发过程,必须在控制台查看输出,有经验的程序员通过控制台输出就能知道整个数据交互的一个流程.但是一个发布的程序,里面带有太多的NSLog输出,肯定对于App性能有所影响,这时候我们可以使用一个宏定义来处理,在开发的时候使用DEBUG模式,在发布的时候使用RELEASE模式.这样,发布的App就不会在程序内部做大量的NSLog输出了. 简单的代码如下, #if defined(DEBUG)||defined(_DEBUG)     NS

iOS开发UI篇—UIPickerView控件简单介绍

iOS开发UI篇—UIPickerView控件简单介绍 一.UIPickerView 控件 1.简单介绍: 2.示例代码 TXViewController.m文件 1 // Created by 鑫 on 14-10-15. 2 3 // Copyright (c) 2014年 梁镋鑫. All rights reserved. 4 5 // 6 7 8 9 #import "TXViewController.h" 10 11 12 13 @interface TXViewContro

iOS 开发技巧

快速移除键盘 之前一直是讲view的父类改成control,然后加一个全屏按钮,点击让键盘消失.这个方法要写好多代码,现在一个比较好的方法是重写一个下面的方法: ? 1 2 3 4 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {     [self.view endEditing:YES]; } iOS 开发技巧,布布扣,bubuko.com

IOS开发技巧(转)

Subclassing 继承/子类 大多语言允许开发者子类化框架所提供的类,但是在 Objective-C 中不完全是这样.大部分常用的类,诸如 NSArray.NSSet.NSDictionary 基本上都是集合类型的.不建议继承这些类,除非你准备转发调用或者实现所有必要的原始方法. 在传统的开发语言中,通常会通过继承基础类型(类似 NSArray 的类)来新增方法,重载已有的方法,或是自定义 UI 组件的外观.在 Objective-C 中,一般通过 Category 来扩展新方法.通过混合

iOS开发练习之UIPickerView实现歌词翻滚效果

麻雀虽小,五脏俱全.在平时的项目中,任何一个模块或者功能里其实都隐藏着许多我们平时注意不到的知识点,其实很多东西大家每天都在用,但很多时候都是知其然,而不知其所以然.时间久了,也就懒得去想到底是什么原因了,怎么实现的之类.回想自己的学习路程,也基本都这样混过来,实在愧对光阴,近日抽空,查看过往笔记,顺手写了个小代码练习,感觉温故知新.现分享代码,以供新手入门参考,尤其其中错误的部分也很有广泛性.同时也欢迎各路成精的老鸟们喷吐,能够指正,这样也促进我再进步一点. ViewController.m文

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

UIAlertView是iOS开发过程中最常用的控件之一,是提醒用户做出选择最主要的工具.在iOS8及后来的系统中,苹果更推荐使用UIAlertController来代替UIAlertView.所以本文也并不提倡开发者再使用UIAlertView,本文的目的是探讨如何将原来的给变量赋值和通过Delete来回调的方式变成链式编程风格和通过Block来回调.通过学习对UIAlertView的改造让各位iOS开发者能够学会这种更加便捷的开发方式 什么是链式编程 对于有一定开发经验的开发者来说,链式编程