生活随笔
收集整理的這篇文章主要介紹了
IOS开发高级之点餐系统的实现-01
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS開發高級之點餐系統的實現-01
#import "ViewController.h"@interface ViewController
() <UIPickerViewDataSource
,UIPickerViewDelegate
>@property(nonatomic
,strong
)NSArray
*foods
;@property (weak
, nonatomic
) IBOutlet UILabel
*lblFruit
;@property (weak
, nonatomic
) IBOutlet UILabel
*lblMainFood
;@property (weak
, nonatomic
) IBOutlet UILabel
*lblBar
;- (IBAction
)randomBtn
:(id
)sender
;@property (weak
, nonatomic
) IBOutlet UIPickerView
*pickerView
;@end@implementation ViewController
- (void)pickerView
:(UIPickerView
*)pickerView didSelectRow
:(NSInteger
)row inComponent
:(NSInteger
)component
{NSString
*selFood
= self.foods
[component
][row
];NSLog(@"%@",selFood
);switch (component
) {case 0:_lblFruit
.text
= selFood
;break;case 1:_lblMainFood
.text
= selFood
;break;case 2:_lblBar
.text
= selFood
;break;}}- (NSString
*)pickerView
:(UIPickerView
*)pickerView titleForRow
:(NSInteger
)row forComponent
:(NSInteger
)component
{NSArray
*comFoods
= self.foods
[component
];NSString
*food
= comFoods
[row
];return food
;}- (NSInteger
)numberOfComponentsInPickerView
:(UIPickerView
*)pickerView
{return self.foods
.count
;}- (NSInteger
)pickerView
:(UIPickerView
*)pickerView numberOfRowsInComponent
:(NSInteger
)component
{return [self.foods
[component
] count
];}- (NSArray
*)foods
{if(_foods
==nil
){_foods
= [NSArray arrayWithContentsOfFile
:[[NSBundle mainBundle
] pathForResource
:@"foods.plist" ofType
:nil
]];}return _foods
;}- (void)viewDidLoad
{[super viewDidLoad
];for(int i
=0;i
<self.foods
.count
;i
++){[self pickerView
:self.pickerView didSelectRow
:0 inComponent
:i
];}}- (IBAction
)randomBtn
:(id
)sender
{for(int i
=0; i
<self.foods
.count
;i
++){NSUInteger count
= [self.foods
[i
] count
];u_int32_t radNum
=arc4random_uniform((int)count
);NSInteger selRowNum
= [self.pickerView selectedRowInComponent
:i
];while (selRowNum
==radNum
) {radNum
=arc4random_uniform((int)count
);}[self.pickerView selectRow
:radNum inComponent
:i animated
:YES
];[self pickerView
:self.pickerView didSelectRow
:radNum inComponent
:i
];}}
@end
總結
以上是生活随笔為你收集整理的IOS开发高级之点餐系统的实现-01的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。