zoukankan      html  css  js  c++  java
  • iOS pickerView

    #import "ViewController.h"
    
    @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
    @property (nonatomic,strong)NSArray *foods;
    //显示水果
    @property (weak, nonatomic) IBOutlet UILabel *fruitLabel;
    //显示主食
    @property (weak, nonatomic) IBOutlet UILabel *mainLabel;
    //显示饮料
    @property (weak, nonatomic) IBOutlet UILabel *drinkLable;
    @property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
    //随机
    - (IBAction)radom:(id)sender;
    //确认
    - (IBAction)certain:(id)sender;
    //取消
    - (IBAction)cancel:(id)sender;
    
    @end
    
    @implementation ViewController
    
    //懒加载数组
    -(NSArray *)foods{
        if (_foods==nil) {
            _foods=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"foods" ofType:@"plist"]];
        }
        return _foods;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        for (int i=0; i<3; i++) {
            [self pickerView:nil didSelectRow:0 inComponent:i];
        }
    }
    
    //总共多少列
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return self.foods.count;
    }
    
    //总共多少行
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        NSArray *subFood=self.foods[component];
        return subFood.count;
    }
    
    //第component列第row行显示文字内容
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        return self.foods[component][row];
    }
    
    //选中第component列第row行
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
        //选中行的文本显示对应的Label
        if(component==0){
            self.fruitLabel.text=self.foods[component][row];
        }else if(component==1){
            self.mainLabel.text=self.foods[component][row];
        }else if(component==2){
            self.drinkLable.text=self.foods[component][row];
        }
    }
    
    //设置行高
    -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
        return 35;
    }
    
    //随机一份套餐
    - (IBAction)radom:(id)sender {
        for (int component=0;component<self.foods.count;component++){
            //第compnent列数组的总长度
            int count=(int)[self.foods[component]count];
            //旧的行号
            int oldRow=(int)[self.pickerView selectedRowInComponent:component];
            //第几行,默认旧行号与新行号相同
            int row=oldRow;
            //保证行号与上次不一样
            while(row==oldRow){
                row=arc4random()%count;
            }
            //让pickerView主动选中第compoent列的第row行
            [self.pickerView selectRow:row inComponent:component animated:YES];
            //设置label的文字
            [self pickerView:nil didSelectRow:row inComponent:component];
        }
    }
    
    - (IBAction)certain:(id)sender {
        //提示用户点餐成功
        UIAlertController *alertC=[UIAlertController alertControllerWithTitle:@"点餐成功" message:@"请稍后..." preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *certain=[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
        [alertC addAction:certain];
        [self presentViewController:alertC animated:YES completion:nil];
    }
    
    - (IBAction)cancel:(id)sender {
        //直接初始化
        for(int i=0;i<3;i++){
            [self pickerView:nil didSelectRow:0 inComponent:i];
            [self.pickerView selectRow:0 inComponent:i animated:YES];
        }
    }
    @end
  • 相关阅读:
    eclipse修改web项目部署路径
    Jquery面试题
    23中设计模式之单例模式
    详细探讨单例模式
    java常用设计模式
    vue官网总结
    pytorch模型训练加速tricks
    element table显示滚动条
    vue中less文件全局引用
    vue路径别名无法识别,Cannot find module
  • 原文地址:https://www.cnblogs.com/hahayixiao/p/15759075.html
Copyright © 2011-2022 走看看