首页 > 移动平台 > 详细

iOS开发 点菜系统 使用UIPickerView

时间:2014-06-05 15:10:23      阅读:462      评论:0      收藏:0      [点我收藏+]

利用UIPickerView实现一个简单的点菜界面,如下图所示。源码地址:https://github.com/xiaoLong1010/iOSAppDemo.git

bubuko.com,布布扣

UIPickerView有两个协议UIPickerViewDataSource,UIPickerViewDelegate,它们为UIPickeView提供数据和代理。UIPickeView的三个component分别代表fruit,main,drink,选中某一行,菜名在下面显示。最上面的随机按钮,则实现随机点菜功能。

1.viewController实现两个协议,并且引用相关数据和控件。

bubuko.com,布布扣
@interface CsyViewController () <UIPickerViewDataSource,UIPickerViewDelegate>

@property (strong,nonatomic) NSArray *foodGroups;

@property (weak, nonatomic) IBOutlet UIPickerView *picker;
@property (weak, nonatomic) IBOutlet UILabel *fruitLabel;
@property (weak, nonatomic) IBOutlet UILabel *mainLabel;

@property (weak, nonatomic) IBOutlet UILabel *drinkLabel;
@property (weak, nonatomic) IBOutlet UIButton *randBtn;
@end
bubuko.com,布布扣

2.数据保存在plist文件中,由foodGroup引用,并且使用懒加载读取数据。

bubuko.com,布布扣
//重写getter方法,懒加载实现
- (NSArray *)foodGroups
{
    if (!_foodGroups) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil];
        
        _foodGroups = [NSArray arrayWithContentsOfFile:path];

    }
    
    return _foodGroups;
    
}
bubuko.com,布布扣

3.UIPickerViewDataSource提供的相关信息:component数以及每个component有多少行

bubuko.com,布布扣
#pragma mark -- UIPickerViewDataSource

//返回component数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.foodGroups.count;
}

//返回每个component有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.foodGroups[component] count];
}
bubuko.com,布布扣

4.UIPickerViewDelegate提供每一行显示的数据。

bubuko.com,布布扣
#pragma mark -- UIPickerViewDelegate

//每一行显示的数据,字符串数据

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.foodGroups[component][row];
}
bubuko.com,布布扣

5.在UIPickerView选中某一行后,需要动态更改下方的菜名。这个是通过UIPickerViewDelegate协议的didSelectRow:方法实现的,当选中某一行,会自动调用该方法。即在该方法中动态地改变菜名。

bubuko.com,布布扣
- (void)viewDidLoad
{
    [super viewDidLoad];
//    for (NSArray *group in self.foodGroups) {
//        CsyLog(@"%@",group);
//    }
    
    //初始化三个label,菜单名
    for (int i = 0; i < self.foodGroups.count; i++) {
        [self pickerView:self.picker didSelectRow:4 inComponent:i];
    }
    
//    NSInteger row = [self.picker selectedRowInComponent:0];
//    CsyLog(@"selected row = %d",row);
    
    //监听方法
    [self.randBtn addTarget:self action:@selector(randBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

#pragma mark --randBtn Listener

- (void)randBtnDidClick:(UIButton *)button
{
//    CsyLog(@"click");
    
    for (int i = 0; i < self.foodGroups.count; i++) {
        NSArray *group = self.foodGroups[i];
        NSInteger newRow = arc4random() % group.count;
        //之前被选中的行
        NSInteger currentRow = [self.picker selectedRowInComponent:i];
//        NSLog(@"currentRow = %d",currentRow);
        //如果随机值和当前的值相当,随机的值加1
        if (newRow == currentRow) {
            newRow = (newRow + 1) % group.count;
        }
//        NSLog(@"newRow = %d",newRow);
        //选中某行某列
        [self.picker selectRow:newRow inComponent:i animated:YES];
        //调用didSecled方法,更新三个label
        [self pickerView:self.picker didSelectRow:newRow inComponent:i];
    }

    
    
}
bubuko.com,布布扣

iOS开发 点菜系统 使用UIPickerView,布布扣,bubuko.com

iOS开发 点菜系统 使用UIPickerView

原文:http://www.cnblogs.com/csy8752316/p/3768966.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!