zoukankan      html  css  js  c++  java
  • UI :使用 UIPickerView 来选择数据

    问题:想让用户在程序中从一个列表中选择数据。 

    通过 Picker view 可以显示一系列的值给用户,并且可以让用户选择其中一个。iPhone 中 Clock 程序里面的时间选择就是一个非常好的例子 

     .h文件:

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
    
    @property(nonatomic,strong)UIPickerView *myPicker;
    
    @end

    .m:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = [UIColor whiteColor];
        self.myPicker = [[UIPickerView alloc]init];
        self.myPicker.center = self.view.center;
        
        self.myPicker.delegate = self;//设置代理
        self.myPicker.showsSelectionIndicator = YES;
        [self.view addSubview:self.myPicker];
        //此时pickerView没有数据,效果不好.
        ///为选择器添加数据:首先给 picker view 指定一个 data source,然后确保 view controller 遵循相关的协议。UIPickerView 的 data source 实例必须遵循 UIPickerViewDataSource 协议。
    }
    
    //UIPickerViewDataSource @required协议方法的实现
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        //这个方法主要是用来为你的选择器添加组件的,如果你需要多个组件,你可以在这个里面进行添加.
        return 1;
    }
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        //顾名思义,这个方法表示你的一个组件中有多少个选项。
        return 10;
    }
    //UIPickerViewDelegate 协议方法
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        NSString *result = nil;
        if ([pickerView isEqual:_myPicker]) {
            result = [NSString stringWithFormat:@"Row%ld",row+1];
        }
        return result;
    }

    如图:

    我们现在不能检测到用户实际选择的项 ,获得用户选择的数据。我们可以通过调用 UIPickerViewselectedRowIncomponent:方法。 
     
    如果程序在运行时,你需要修改picker view中的值,那么需要从data source和delegate 中 reload 相关数据。可以通过使用 reloadAllComponent:方法来强制重新加载所有数据,或者 使用 reloadComponent:方法来加载指定组件的数据。 

       (单个组件选取器,我们创建一个数组NSArray来保存选取器中的内容;选取器本身不会储存任何数据,它通过调用数据源和委托方法来显示数据;但是对于大量数据的数据源,数组并不合适,我们可以做一个静态列表如plist文件或者URL载入)

    //获取用户所选行的数据
    
    UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        selectButton.frame = CGRectMake(100, 100, 80, 35);
        [selectButton setTitle:@"确定" forState:UIControlStateNormal];
        [selectButton addTarget:self action:@selector(selectRowOfData:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:selectButton];
    
    
    - (void)selectRowOfData:(id)sender{
        //参数为component的序号,返回被选中row的序号,若无row被选中,则返回-1
        NSInteger row = [_myPicker selectedRowInComponent:0];
        NSLog(@"%ld",row+1);//这里如果数据存在数组,文件中等,用Row来取出
    }
  • 相关阅读:
    和大家分享下我的找工作历程。
    Traits 编程技法+模板偏特化+template参数推导+内嵌型别编程技巧
    SGI STL空间配置器和内存池
    调试Release版本应用程序
    HawkNL 源码剖析
    C++:float 转型到 std::string 之总结。
    将一个数上调至2^n的倍数《参考STL源码》
    JavaScript中Ajax的使用
    jQuery之noConflict() 方法
    Linq to Object 的简单使用示例
  • 原文地址:https://www.cnblogs.com/safiri/p/4013903.html
Copyright © 2011-2022 走看看