//
// ViewController.m
// 01-点菜系统
//
// Created by panba on 16-4-12.
// Copyright (c) 2016年 panba. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic,strong)NSArray *foods;
@property(nonatomic,strong)UIPickerView *pickView;
@property(nonatomic,weak) UILabel *label4;
@property(nonatomic,weak) UILabel *label5;
@property(nonatomic,weak) UILabel *label6;
@end
@implementation ViewController
//1-懒加载
-(NSArray *)foods
{
if (_foods == nil) {
//1-找到路径
NSString *fullPath = [[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil];
_foods = [NSArray arrayWithContentsOfFile:fullPath];
}
return _foods;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1-顶部视图
UIView *headerView = [[UIView alloc]init];
headerView.frame = CGRectMake(0, 0, 320, 64);
headerView.backgroundColor = [UIColor blueColor];
[self.view addSubview:headerView];
//1.1增加按钮
UIButton *rodomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
rodomBtn.frame = CGRectMake(20, 20, 64, 20);
[rodomBtn setTitle:@"随机" forState:UIControlStateNormal];
[rodomBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[rodomBtn addTarget:self action:@selector(randomFood) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:rodomBtn];
//1.2软件label
UILabel *titleLabel = [[UILabel alloc]init];
titleLabel.frame = CGRectMake(140, 20, 100, 20);
titleLabel.tintColor = [UIColor blackColor];
titleLabel.text = @"点菜系统";
[headerView addSubview:titleLabel];
//2-设置中部视图
UIView *middleView = [[UIView alloc]init];
middleView.frame = CGRectMake(0, 64, 320, 216);
middleView.backgroundColor = [UIColor grayColor];
[self.view addSubview:middleView];
//2.1增加pickview
UIPickerView *pickView = [[UIPickerView alloc]init];
pickView.frame = CGRectMake(0, 0, 320, 216);
[middleView addSubview:pickView];
pickView.dataSource = self;
pickView.delegate = self;
self.pickView = pickView;
//3-底部视图
UIView *bottomView = [[UIView alloc]init];
bottomView.frame = CGRectMake(0, 280, 320, 280);
bottomView.backgroundColor = [UIColor redColor];
[self.view addSubview:bottomView];
//3.1添加几个label
UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 40, 20)];
UILabel *lable2 = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, 40, 20)];
UILabel *lable3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 140, 40, 20)];
UILabel *lable4 = [[UILabel alloc]initWithFrame:CGRectMake(100, 20, 100, 20)];
UILabel *lable5 = [[UILabel alloc]initWithFrame:CGRectMake(100, 80, 100, 20)];
UILabel *lable6 = [[UILabel alloc]initWithFrame:CGRectMake(100, 140, 100, 20)];
lable1.text = @"水果";
lable2.text = @"主菜";
lable3.text = @"饮料";
lable1.textColor = [UIColor blackColor];
lable2.textColor = [UIColor blackColor];
lable3.textColor = [UIColor blackColor];
lable4.textColor = [UIColor blackColor];
lable5.textColor = [UIColor blackColor];
lable6.textColor = [UIColor blackColor];
self.label4 = lable4;
self.label5 = lable5;
self.label6 = lable6;
[bottomView addSubview:lable1];
[bottomView addSubview:lable2];
[bottomView addSubview:lable3];
[bottomView addSubview:lable4];
[bottomView addSubview:lable5];
[bottomView addSubview:lable6];
//设置pickview的默认选择行数
for (int component = 0; component < self.foods.count; component++) {
}
}
//2-设置pickview返回多少组
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return self.foods.count;
}
//2.1设置每一组返回多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray *foodsModel = self.foods[component];
return foodsModel.count;
}
//2.3设置每一组的每一行都显示什么内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSArray *foodsModel = self.foods[component];
NSString *title = foodsModel[row];
return title;
}
//3-重写didselect方法
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *name = self.foods[component][row];
if (component ==0) {
self.label4.text = name;
}else if (component == 1)
{
self.label5.text = name;
}else if (component ==2)
{
self.label6.text = name;
}
}
//3-重新随机都方法
-(void)randomFood
{
for (int component = 0; component <self.foods.count; component++) {
// 获取对应列的数据总数
int total = [self.foods[component] count];
// 根据每一列的总数生成随机数(当前生成的随机数)
int randomNumber = arc4random() % total;
// 获取当前选中的行(上一次随机后移动到的行)
int oldRow = [self.pickView selectedRowInComponent:0];
while (randomNumber == oldRow) {
randomNumber = arc4random() % total;
}
// 让pickerview滚动到某一行
[self.pickView selectRow:randomNumber inComponent:component animated:YES];
//
[self pickerView:nil didSelectRow:randomNumber inComponent:component];
}
}
@end
ios pickerView 点菜
最新推荐文章于 2024-09-05 21:10:43 发布