码迷,mamicode.com
首页 > 其他好文 > 详细

点餐系统思路

时间:2014-12-05 22:49:54      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   color   sp   for   strong   on   文件   

点餐系统思路

步骤一:遵守和实现UIPickerView的数据源。

步骤二:加载plist文件,把数据存放在NSArray数组中。
- (NSArray *)foodArray
{
   
if (_foodArray == nil) {
        _foodArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:
@"foods.plist" ofType:nil]];
       
for (int i = 0; i < self.foodArray.count; i++) {
            [
self pickerView:nil didSelectRow:0 inComponent:i];
        }
    }
   
return _foodArray;
}

步骤三:遵守和实现UIPickerView的代理方法,监听选中每一行的方法。
#pragma mark - 实现代理方法

/** 一共有多少组 */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
   
return self.foodArray.count;
}

/** 一组有多少行 */
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSArray *foods =
self.foodArray[component];
   
return foods.count;
}

/** 一行显示什么内容 */
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
   
return  self.foodArray[component][row];
}

/** 选中 */
- (
void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   
if (component == 0) {
       
self.fruitLabel.text = self.foodArray[component][row];
    }
else if(component == 1){
       
self.mainFoodLabel.text = self.foodArray[component][row];
    }
else if(component ==2){
       
self.drinkLabel.text = self.foodArray[component][row];
    }
}

步骤四:监听UIBarButtonItem随机的事件
- (
IBAction)randomClickBtn
{
   
for (int i = 0;i < self.foodArray.count;i++) {
       
// 根据组算出有多少行
       
int cou = [self.foodArray[i] count];
       
// 取得被选中的
       
int oldRow = [self.picker selectedRowInComponent:i];
       
int row = oldRow;
       
// 如果上一个和下一个相等,就随机
       
while (row == oldRow) {
            row = arc4random_uniform(cou);
        }
       
        [
self.picker selectRow:row inComponent:i animated:YES];
        [
self pickerView:nil didSelectRow:row inComponent:i];
    }
}

点餐系统思路

标签:style   io   ar   color   sp   for   strong   on   文件   

原文地址:http://blog.csdn.net/itcontend/article/details/41758363

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!