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-10-14 21:28:30

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

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开发技巧 - 使用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开发技巧之查看模拟器沙盒文件

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

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

0. 背景: 在iOS开发中,Auto Layout(自动布局)能解决大部分的屏幕适配问题. 但是当iPhone 6和iPhone 6 Plus发布以后, Auto Layout已经不能解决复杂的屏幕适配问题了, 因此, 在iOS 8以后苹果推出了Size Class, 它是基于Auto Layout技术的.   1. Size Class的开启和使用 与Auto Layout不同, Size Class不能通过代码编程管理,只能通过IB(Interface Builder)使用. 默认情况下S

iOS开发技巧 - 使用Alerts和Action Sheets显示弹出框

解决方案: (Swift) 使用UIAlertController类   (Objective-C) 使用UIAlertView类   代码: (Swift) import UIKit class ViewController: UIViewController { // 1. define the variable that will hold our alert controller var controller:UIAlertController? override func viewDi

iOS开发技巧 - 使用UISegmentedControl来对信息进行分组

      (Swift) import UIKit class ViewController: UIViewController { var segmentedControl:UISegmentedControl! override func viewDidLoad() { super.viewDidLoad() let segments = [ "iPhone", "iPad", "iPod", "iMac"] segme

iOS开发技巧 - 使用和定制开关控件(UISwitch)

1. 初始化加载到视图界面 (Swift) import UIKit class ViewController: UIViewController { // 1. create a property of type UISwitch var mainSwitch:UISwitch! override func viewDidLoad() { super.viewDidLoad() // 2. create switch mainSwitch = UISwitch(frame: CGRect(x:

分享一些iOS开发实用的小技巧_IOS

1.设置navigationbar title颜色 UIColor *whiteColor = [UIColor whiteColor]; NSDictionary *dic = [NSDictionary dictionaryWithObject:whiteColor forKey:NSForegroundColorAttributeName]; [self.navigationController.navigationBar setTitleTextAttributes:dic]; 2.获取

iOS开发入门:iOS视图生命周期与视图控制器关系

iOS中视图是一个应用的重要组成部分,功能的实现与其息息相关,而视图控制器控制着视图,其重要性在整个应用中不言而喻. 视图生命周期与视图控制器关系 以视图的5种状态为基础,我们来系统的了解一下视图控制器的生命周期.在视图不同的生命周期,视图控制器会回调不同的方法. 开发入门:iOS视图生命周期与视图控制器关系-"> viewDidLoad方法:视图控制器已被实例化,在视图被加载到内存中的时候调用该方法,这个时候视图并未出现.在该方法中通常进行的是对所控制的视图进行初始化处理. 视 图可见前